Home > Net >  How to make query for some fields of the models?
How to make query for some fields of the models?

Time:12-19

I am makine business directory and i stucked in city and districts pages of business directory.

I made a simple business directory. All i want three queries; Forexample category name: Manufacturing

First: I want one page like this: abc.com/directory/category/slug:slug(this slug means category name)/ ( I did, i have no problem with this page.

Second: City url like this abc.com/directory/category/slug:slug/(city name with slug style)/. forexample abc.com/category/manufacturing/london/

Third: District url like this abc.com/directory/category/slug:slug/(city name with slug style)/. forexample abc.com/category/manufacturing/london/southwark(district names)

I only did first one and second and third made me crazy...

I tried lots of things but failed...

Here are my codes:

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)

urls. 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')),

CodePudding user response:

If i understood you correctly...

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) 

There are two different approaches for dealing with errors such as someone intentionally entering other values in browser url. Hope it helps.

  • Related