Django Sub Filter Brings Same Datas with Main Filter

I am making very simple business directory. I wanna display a list of firms that belong to a particular city and district, and belong to a certain category. But district filter and city filter brings same datas. How can i fix this? I filtered to district but again brings main city datas. Where is my fault? enter image description here

enter image description here

models.py

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

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)


class FirmaEkle(CreateView):
    model = Firma
    form_class = FirmaForm
    template_name = 'firma/add_firma.html' 


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



def firmadistrict(request, city_slug, dist_slug, category_slug):
    category = FirmaCategory.objects.get(slug=category_slug)
    city = City.objects.get(slug=city_slug)
    if city is not None:
        district = District.objects.filter(city=city)
        if district is not None:
            firms = Firma.objects.filter(district__in=district)
            context = {
                "firms" :firms
            }
            return render(request, 'firma/district.html', context) 


    context = {
        "error" : "Error in city/district" 
    } 
    return render(request, 'firma/district.html', context) 

     
Back to Top