django 如何对模型的某些字段进行查询?

wfveoks0  于 2022-12-20  发布在  Go
关注(0)|答案(1)|浏览(161)

我是makine的商业目录,我在城市和地区的商业目录页卡住了。
我做了一个简单的企业目录。所有我想要三个查询;例如,类别名称:制造业

    • 第一页:**我想要这样的一页:slug(这个slug的意思是类别名称)/(我做了,我对这个页面没有问题。
      **Second:**City url like this abc.com/directory/category/slug:slug/(city name with slug style)/. forexample abc.com/category/manufacturing/london/
    • 第三个:**地区网址如下:abc.com/directory/category/slug:slug/(带有鼻涕虫样式的城市名称)/。例如:abc.com/category/manufacturing/london/southwark(地区名称)

我只做了第一个第二个和第三个让我发疯...
我试过很多方法都失败了...
下面是我的代码:
models.py

from django.db import models
from django.urls import reverse
from tinymce import models as tinymce_models

# Create your models here.

class City(models.Model):

        city = models.CharField(max_length=50)
        slug = models.SlugField()

        def __str__(self):
            return str(self.city)

class District(models.Model):

        district = models.CharField(max_length=50)
        slug = models.SlugField()
        city = models.ForeignKey(City, on_delete=models.CASCADE)    

        def __str__(self):
            return str(self.district)



class FirmaCategory(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField()

    class Meta:
        verbose_name_plural = 'Firma Kategorileri'

    def __str__(self):
        return str(self.title)


class Firma(models.Model):
    category = models.ForeignKey(FirmaCategory, related_name="Firma", on_delete=models.CASCADE)
    title = models.CharField(max_length=255)
    slug = models.SlugField()
    adres = tinymce_models.HTMLField()
    tel = tinymce_models.HTMLField()
    website = tinymce_models.HTMLField()
    email = tinymce_models.HTMLField()
    intro = tinymce_models.HTMLField()
    body = tinymce_models.HTMLField()
    city = models.ForeignKey(City,related_name="FirmaCategory" ,on_delete=models.CASCADE)
    district = models.ForeignKey(District,related_name="FirmaCategory", on_delete=models.CASCADE)
    #url = models.CharField(max_length=255)

    date_added = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural = 'Firmalar'

    def __str__(self):
        return str(self.title)

    def get_absolute_url(self):
        return reverse('firma-frontpage')

views.py

from django.db.models import Q
from django.shortcuts import render
from .models import Firma, FirmaCategory, City, District
from django.utils.html import format_html
from django.views.generic import CreateView
from .forms import FirmaForm

# Create your views here.
def firmaana(request):
    firmalar = Firma.objects.all()
    context = {
        'firmalar': firmalar
    }
    return render(request, 'firma/firma-frontpage.html', context)

def firmadetail(request, slug):
    firmax = Firma.objects.get(slug=slug)
    context = {
        'firmax': firmax,
    }  
    return render(request, 'firma/firma-detail.html', context)

def firmacategory(request, slug):
    firmacategory = FirmaCategory.objects.get(slug=slug)
    context = {
        'firmacategory': firmacategory
    }  
    return render(request, 'firma/firma-category.html', context)

网址. py

urlpatterns = [
    
    path('kategory/city/<city>/', views.list_businesses_by_city, name='firmail'),
    path("", views.firmaana, name="firma-frontpage"),
    path("<slug:slug>/", views.firmadetail, name="firma-detail"),
    path("kategory/<slug:slug>/", views.firmacategory, name="firma-category"),
    path("1/add-firma/", FirmaEkle.as_view(), name="firma-ekle"),
    path('tinymce/', include('tinymce.urls')),
gxwragnw

gxwragnw1#

如果我没理解错的话...
views.py

def firmacity(request, city_slug):
    try:
        city = City.objects.get(slug=city_slug)
        context = {
        "firma" : Firma.objects.filter (city=city)
        }
        return render(request, tmpl, context) 
     except City.DoesNotExist:
         context = {
             "error" : "City does not exist" 
         }
         return render(request, tmpl, context) 

def firmadistrict(request, city_slug, dist_slug):

    city = City.objects.get(slug=city_slug)
    if city is not None:
        district = District.obects.get(city=city)
        if district is not None:
            firma = Firma.objects.filter(district=district)
            context = {
                "firma" :firma, 
            }
            return render(request, tmpl, context)

    context = {
        "error" : "Error in city/district" 
    } 
    return render(request, tmpl, context)

有两种不同的方法来处理错误,比如有人故意在浏览器url中输入其他值。希望能有所帮助。
我将在这里编辑你的第二个问题。所以如果我理解正确。你要检索公司数据的类别。这样你就可以访问字段(城市,地区)的公司在确切的公司类别。
views.py

def firmabycategory(request, cat_slug):
    try:
        category = FirmaCategory.objects.get(slug=cat_slug)
        companies = Firma.objects.filter(category=category)
        context = {
            'companies': companies,
        }
        return render(request, tmpl, context)
    except FirmaCategory.DoesNotExist:
        context = {
            'error': 'Selected Category does not exist!',
        }
        return render(request, tmpl, context)

模板(html):

{% for company in companies %}
    name: {{ company.title }}
    category: {{ company.category }}
    city: {{ company.city }}
    district: {{ company.district }}
{% endfor %}

相关问题