I try to use django-filters in my Django app. I am using it with pagination. Page renders properly (it just works), but without filter fields that I created. Only submit button shows. I will be really thankful for any help i don't know what I am doing wrong.
I want to use ModelChoiceFilter to filter objects from Image model, by their foreign key Publisher
filters.py
# filters.py
import django_filters
from django import forms
from .models import Image, Publisher, Author
class ImageFilter(django_filters.FilterSet):
class Meta:
model = Image
fields = ['title', 'created']
class ImageForeignFilter(django_filters.FilterSet):
wydawca = django_filters.ModelChoiceFilter(
queryset=Publisher.objects.all(),
empty_label="Wszyscy wydawcy",
label="Wydawca",
widget=forms.Select(attrs={'class': 'form-control'}),
)
class Meta:
model = Image
fields = ['created', 'wydawca']
models.py
# models.py
class Publisher(models.Model):
name = models.CharField(max_length=200, default='',blank=True, verbose_name="Nazwa wydawcy", help_text="Nazwa wydawcy")
urlglowna = models.URLField(max_length=2000, default='', blank=True, verbose_name="Strona główna", help_text="Adres strony")
def __str__(self):
return '{} {}'.format(self.name,self.urlglowna)
class Image(models.Model):
url = models.URLField(max_length=2000, verbose_name="Link do znaleziska", help_text="Podaj bezpośredni link")
created = models.DateField(auto_now_add=True)
publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE, null = True, blank=True)
views.py
# views.py
def image_list(request):
images = ImageForeignFilter(
request.GET, queryset=Image.objects.filter(status=Image.Status.PUBLISHED).order_by('created')
).qs
paginator = Paginator(images, 15)
page = request.GET.get('page')
try:
page_number = paginator.page(page)
except PageNotAnInteger:
page_number = paginator.page(1)
except EmptyPage:
page_number = paginator.page(paginator.num_pages)
return render(request, 'images/image/list.html', {'section': 'images','images': images, 'page_number': page_number})
template
# template
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% load i18n %}
{% block title %}Title{% endblock %}
{% block content %}
<div id="image-list">
<form method="get">
{{ images.form.as_p }}
<input type="submit"/>
</form>
{% for user in images.qs %}
{{ user.wydawca }}
{% endfor %}
{% include "images/image/list_images.html" %}
{% include "pagination.html" with page=images %}
</div>
{% endblock %}
Ok, it works for me:
#template
<form method="get">
{{ images_filter.form.as_p }}
<input type="submit" value="Filtruj"/>
</form>
#filters
class ImageForeignFilter(django_filters.FilterSet):
publisher = django_filters.ModelChoiceFilter(
queryset=Publisher.objects.all(),
empty_label="Wszyscy wydawcy",
label="Wydawca",
widget=forms.Select(attrs={'class': 'form-control'}),
)
class Meta:
model = Image
fields = ['publisher']
#view
def image_list(request):
qs = Image.objects.filter(status=Image.Status.PUBLISHED).order_by('created')
images_filter = ImageForeignFilter(request.GET, queryset=qs)
paginator = Paginator(images_filter.qs, 15)
page = request.GET.get('page')
try:
images = paginator.page(page)
except PageNotAnInteger:
images = paginator.page(1)
except EmptyPage:
images = paginator.page(paginator.num_pages)
return render(request, 'images/image/list.html', {'section': 'images','images': images, 'images_filter': images_filter})