将 django-filter 与 class DetailView 一起使用

Using django-filter with class DetailView

我想用 Django 和 django-filter 制作一个动态过滤表单,比如 Whosebug 过滤器

我想在由 DetailView 呈现的模板上使用此过滤器,现在的问题是我搜索了很多但没有找到如何实现此目的的示例

在我的模板中,我列出了与 'materials' class 相关的所有 'courses' 我想在同一个模板上制作一个表格,这样我就可以过滤这个列表

我的模板

{% for course in material_detail.courses_set.all %}
<div class="course">
    <h5>{{ course.course_name }}</h5>
    <p>{{ course.course_description }} </p>            
</div>
<hr/>
{% endfor %}

我有两个模型 'Materials' 和 'Courses' 我的 models.py

class Materials(models.Model):
    materials_name          = models.CharField(max_length=200)
    materials_description   = models.CharField(max_length=200, null=True)
    slug_material           = models.SlugField(blank=True, unique=True)


class Courses(models.Model):
    course_name             = models.CharField(max_length=200)
    course_description      = models.CharField(max_length=300, null=True)
    material                = models.ForeignKey(Materials, on_delete = models.CASCADE)

我有一个 Class DetailView 我的 views.py

class MaterialsDetailView(DetailView):
    model               = Materials
    template_name       = 'tuts/material.html'
    context_object_name = 'material_detail'

我制作了一个 filters.py 文件并在上面制作了这段代码,但我不知道如何 link 使用我的 Class DetailView

import django_filters

from .models import Materials

class CourseFilter(django_filters.FilterSet):

class Meta:
    model = Materials
    fields = ['courses__course_name', 'courses__course_description']

注: 我的模型和模板上有更多代码我将其删除以使代码非常简单

您可以改用 ListView 并使用 get_queryset() 进行过滤,然后调用 get_context_data() 以 return 关键字参数作为模板的上下文。

然后只需为您需要的每个过滤器连接一个单独的 URL conf。您可以为每个视图使用相同的模板,只需 link 到 URL conf 并在模板中使用 href,将 URL 参数作为关键字参数传递并使用 [=20 访问它=]['name_of_parameter'] 在你看来。然后在视图中按该参数过滤以获得查询集,然后 return 返回模板。

博客示例:

class PostCategoryFilter(ListView):
    context_object_name = 'posts'
    template_name = 'blog/post_list.html'

    def get_queryset(self):
        return Post.objects.filter(category=self.kwargs['category_id'])

    def get_context_data(self, **kwargs):
        context = super(PostCategoryFilter, self).get_context_data(**kwargs)
        context['categories'] = Category.objects.all()
        context['filtered_category'] = Category.objects.get(id=self.kwargs['category_id'])

        return context

上下文中的任何内容都可以用作模板中的变量,例如,您将拥有类别和过滤后的帖子。

您只是显示与 material 相关的 课程列表。所以你可以用f = CourseFilter(self.request.GET, queryset=Courses.objects.filter(material=material_pk))过滤相关课程。注意我使用 CourseFilter 而不是 MaterialFilter

views.py

class MaterialsDetailView(DetailView):
    model = Materials
    template_name = 'tuts/material.html'
    context_object_name = 'material_detail'

    def get_context_data(self, **kwargs):
        context_data = super(MaterialsDetailView, self).get_context_data()
        material_pk = self.kwargs.get('pk', None)
        f = CourseFilter(self.request.GET, queryset=Courses.objects.filter(material=material_pk))
        context_data['filter'] = f
        return context_data

filters.py

class CourseFilter(django_filters.FilterSet):
    class Meta:
        model = Courses
        fields = []
    course_name = django_filters.CharFilter(field_name="course_name", lookup_expr="icontains")
    course_description = django_filters.CharFilter(field_name="course_description", lookup_expr="icontains")

在您的模板中,您可以访问 filter.qs 中的所有 过滤查询集 filter.form

中的相应表单

tuts/material.html

<form method="GET">
    {{ filter.form.as_p }}
    <input type="submit" />
</form>
{% for course in filter.qs %}
<div class="course">
    <h5>{{ course.course_name }}</h5>
    <p>{{ course.course_description }} </p>
</div>
<hr/>
{% endfor %}