Trying to implement a filter django_filters
to an existing function based view.
The filter is rendering as it should, but nothing is happening.
There is a few question on the topic, so I am going to answer some potential questions:
- I used
$ pip install django-filter
- I installed in my virtual environment
- Filter.py is installed in the same app as the views
Something to mention in my filters.py, import django_filters
is underlined in red ("Import "django_filters" could not be resolved"). I cant find why, but this could be a strong contender in the root cause of the problem.
Settings file
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"crispy_forms",
'main.apps.MainConfig',
'django_filters',
models
class Catalogue(models.Model):
product = models.ManyToManyField('Product', blank=True)
product_title = models.TextField('Product Title', blank=True)
class Meta:
db_table='Catalogue'
def __str__(self):
return str(self.product_title)
filters
import django_filters
from .models import *
class CatalogueFilter(django_filters.FilterSet):
class Meta:
model = Catalogue
fields = ['product_title']
views
from .filters import CatalogueFilter
def show_venue(request, venue_id):
if request.method == "POST" and 'btnreview_form' in request.POST:
form = CatalogueReviewForm(request.POST)
if form.is_valid():
data = form.save(commit=False)
data.product_id = venue_id # links to product ID in Catalogue Model and return product name
data.venue_id = request.POST.get('venue_id') # prints venue ID in form
data.user_id = request.user.id
data.save()
ven_id = request.POST.get('venue_id')
print(data)
form.save()
return HttpResponseRedirect(ven_id)
else:
venue = Venue.objects.get(pk=venue_id)
menu = Catalogue.objects.filter(venue=venue_id)
categories = Catalogue.objects.filter(venue=venue_id).order_by('category_order')
myFilter = CatalogueFilter(request.GET, Catalogue.objects.filter(venue=venue_id).order_by('category_order'))
template (I added a bit more than the basic code, as I am thinking the use of the table could be the problem)
{% if not menu.all %}
No menu available.
{% else %}
<form method="get">
{{myFilter.form}}
<button class ="btn btn-primary" type="submit">
</form>
</br>
{% regroup categories by category_menu as category_menu_list %}
{% for category_menu in category_menu_list %}
<table >
<tr>
<th></th>
<th>
{{category_menu.grouper}}
</th>
</tr>
{% for catalogue in category_menu.list %}
<tr>
<td>{{catalogue.product_title}}
</br>
<div >
<a href="{% url 'show-product-from-venue' catalogue.id %}?next={{ request.path|urlencode}}">Review</a>
</div>
</td>
<td>
</td>
</tr>
{%endfor%}
</table>
{%endfor%}
{%endif%}
requirements
django-filter==22.1
CodePudding user response:
You need to add qs
with filter like this
else:
venue = Venue.objects.get(pk=venue_id)
menu = Catalogue.objects.filter(venue=venue_id)
categories = Catalogue.objects.filter(venue=venue_id).order_by('category_order')
myFilter = CatalogueFilter(request.GET, ,queryset=categories)
categories = myFilter.qs