在Django中使用下拉表单过滤ListView的最佳方法 [英] Best way to filter ListView with drop down form in Django
问题描述
我正在尝试使用下拉列表基于用户过滤ListView.
I'm trying to filter a ListView based on Users, using a drop down form.
models.py
models.py
class Post(models.Model):
...
author = models.ForeignKey('auth.User', verbose_name="Post Author")
views.py
class PostList(ListView):
model = Post
context_object_name = 'posts'
def get_queryset(self):
result = super(PostList, self).get_queryset()
author_filter = self.request.GET.get('author')
if author_filter:
result = Post.objects.filter(Q(author__icontains=author_filter))
return result
post_list.html
post_list.html
<form action="" method="get">
<select name="author" onchange="this.form.submit();">
<option selected="selected" disabled>Select an author</option>
{% all_author as authors %}
{% for author in authors %}
<option value="{{ author }}">{{ author }}</option>
{% endfor %}
</select>
</form>
我正在使用自定义模板标签来呈现all_authors
,并且工作正常.选择作者时,在URL中我可以看到已传递某些内容(/?author = xxx),但未过滤列表.
I am using a custom template tag to render all_authors
and this works fine. When selecting an author, in the urls I can see something is passed (/?author=xxx), but the list is not filtered.
编辑
基于andi的建议,我使用Django过滤器使它以这种方式工作.但是由于某些原因,没有考虑filters.py中的fields = ['field_name',]
,因此我在模板中分别选择字段.
Based on andi's suggestion I made it work this way using django filters. But for some reason fields = ['field_name',]
in filters.py is not taken into account, so I'm selecting the fields individually in the template.
views.py
class PostList(FilterView):
model = Post
filter_class = PostFilter
context_object_name = 'posts'
paginate_by = 50
template_name = 'directory/post_list.html'
filters.py
filters.py
class PostFilter(django_filters.FilterSet):
class Meta:
model = Post
fields = ['author',]
post_list.html
post_list.html
<form action="" method="get">
{{ filter.form.author }}
<input type="submit" />
</form>
编辑2
我发现了为什么未正确传递所选字段的原因,需要在视图filterset_class =
中而不是在filter_class =
I've found out why the selected fields were not passed correctly, needed to use in views filterset_class =
instead of filter_class =
推荐答案
哦,这确实是一种古老的做法,容易出错且耗时.
Ohh, it is really oldschool, error prone and time consuming way of doing the things.
请尝试使用django-filter
库.并以最小的努力创建工作良好的过滤器!这样可以创建非常健壮的过滤策略,同时保持干净的代码.
Please give a try to django-filter
library. And create working fine filters with minimum amount of effort! This allows creating very robust filtering strategies while maintaining clean code.
https://django-filter.readthedocs.io/en /latest/guide/usage.html#
快速草稿以下:
过滤器:
import django_filters
class PostFilter(django_filters.FilterSet):
class Meta:
model = Post
fields = ['author']
视图:
from django_filters.views import FilterView
from somwhere.in.your.project.filtersets import PostFilter
class PostList(FilterView):
model = Post
context_object_name = 'posts'
filter_class = PostFilter
在模板中:
{% extends "base.html" %}
{% block content %}
<form action="" method="get">
{{ filter.form.as_p }}
<input type="submit" />
</form>
{% for obj in filter.qs %}
{{ obj.name }} - ${{ obj.price }}<br />
{% endfor %}
{% endblock %}
这篇关于在Django中使用下拉表单过滤ListView的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!