Listing filter providing unexpected results in django

Viewed 34

I am trying to implement Listing Filter from django filters. First "type" is the attribute that I want my filter to be based inside models.py of my app.

class detaileditems(models.Model):
    title = models.CharField(max_length= 255)
    type = models.CharField(max_length= 45, null=True)
    pubdate = models.DateTimeField()
    body = models.TextField()
    image = models.ImageField(upload_to= 'images/')

I have created a separate filters.py inside my application where I have called the filters.

import django_filters
from .models import detaileditems
class ListingFilters(django_filters.FilterSet):
    class Meta:
        model = detaileditems
        fields = {'type': ['exact']}

Next here is my function inside views.py file-

from .models import detaileditems
from .filters import ListingFilters
def alldetailed2(request):
    items = detaileditems.objects
    listing_filter = ListingFilters(request.GET, queryset=items)
    context = {
        'listing_filter' : listing_filter,
        'items': items,
    }
    return render(request, 'detailed2/detailed2.html',context)

Lastly in my html file "detailed2.html" which is inside the application template folder of "detailed2".

<div class = "col-lg-6 col-md-8 mx-auto">
    <form method = "get">
            {{ listing_filter.form }}
            <button class="btn btn-sm btn-danger" type="submit">Search</button>
         </form>
</div>
<div class = "container">
     <div class = "row row-cols-1 row-cols-sm2 row-cols-md-3 g-3">
          {% for listing in listing_filter.qs %}
          <div class = "col">
               {% url 'alldetailed2' %}
          </div>
           {% endfor %}
      </div>
</div>

I am getting this as the output where the same page is not getting updated with the filtered results.

Filtered output not reappearing

And here is my folder structure for better understanding.

Folder structure

2 Answers

I do see a self reference in the html:

''' {% include "detailed2/detailed2.html" %} '''

could that be the root cause of your trouble?

There is an earlier post on maximum recursion depth: earlier post

Sometimes a solution can be found by rethinking the algorithm so it uses iteration over for example a list instead of recursion. I guess you need to convince yourself you are not making an error and then file an issue with Django.

As a quick fix you can try to increase the recursion depth. See post I linked to.

Related