django-views - 如何使用基于类的 View 和 django-filter 的简单示例?

标签 django-views django-class-based-views django-filter

文档中的示例,https://django-filter.readthedocs.org/en/latest/usage.html ,我认为是基于函数的 View 。我目前正在研究如何使用基于类的 View 来做到这一点。

def product_list(request):
f = ProductFilter(request.GET, queryset=Product.objects.all())
return render_to_response('my_app/template.html', {'filter': f})

最佳答案

多一点挖掘,我已经设法回答它。我已经使用了这里的代码 https://github.com/rasca/django-enhanced-cbv .

我添加了list.py的内容进入我的主应用程序 main_app/filter_mixin.py
然后在应用程序中,我将搜索添加到 ListView 中,我添加了文件 filter.py像这样(与文档相同)

from django_filters import FilterSet
from .models import Contact


class ContactFilter(FilterSet):
    class Meta:
        model = Contact
        fields = ['name_first', 'name_last']

现在view.py变成:
from vanilla import ListView

from .filter import ContactFilter
from galleria.filter_mixin import ListFilteredMixin


class ContactList(ListFilteredMixin, ListView):
    filter_set = ContactFilter

关于django-views - 如何使用基于类的 View 和 django-filter 的简单示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24305854/

相关文章:

django - 为什么获取复杂聚合需要别名错误?

python - Django:CreateView 和 UpdateView 中具有唯一约束的不同行为

django - 使用基于类的 View 将模板的一部分限制为属于组的用户的访问权限。 Django 2.0

python - 如何将隐藏的输入值插入django中的数据库

django-filter - 如何使用graphene-django通过GraphQL中的id列表过滤查询?

django:如何将此 View 迁移到基于类的通用 View 等效项?

python - 基于类的 View VS 基于函数的 View

python - django View 中的简单 for 循环创建 pdf 文件

python - django annotate - 条件计数

django-filter: TypeError at/goods/__init__() 得到一个意外的关键字参数 'name'