在 Django 1.7 中动态过滤 ListView CBV [英] Dynamically filter ListView CBV in Django 1.7

查看:19
本文介绍了在 Django 1.7 中动态过滤 ListView CBV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读关于动态过滤 ListView 的官方文档,但仍然对如何实际使用它感到困惑.

我目前有一个简单的模型,我们称之为Scholarship:

class 奖学金(models.Model):标题=models.CharField(max_length=255)submit_date = models.DateField(auto_now=True,verbose_name='Date Submitted')EXPERIENCE_LEVEL_CHOICES = (('A', '任何'),('S', '学生'),('G', '毕业生'))Experience_level = models.CharField(max_length=1,choices=EXPERIENCE_LEVEL_CHOICES,default='A')

我有一个页面显示所有这些奖学金,使用 ListView:

views.py

from django.views.generic import ListView从 .models 导入奖学金类奖学金目录视图(列表视图):模型 = 奖学金template_name = '奖学金目录.html'

urls.py

from django.conf.urls 导入模式,url从 .views 导入 ScholarshipDirectoryViewurlpatterns = 模式('',url(r'^$', ScholarshipDirectoryView.as_view(), name='scholarship_directory'),)

我正在尝试在网站主页上生成链接,该链接将返回此 ListView 的过滤版本.例如,如果有人点击显示研究生奖学金"链接,则只会显示带有 experience_level='G' 的奖学金.

我通过 shell 返回这个查询集没有问题 -> Scholarship.objects.filter(experience_level__exact='G')

我只是不确定如何通过下拉列表或 URL 动态过滤 ListView.不打算使用插件,而是了解 Django 中动态查询/过滤的工作原理.

解决方案

首先,您需要更改 urls.py,以便它将体验作为参数传递.像这样:

<前>urlpatterns = 模式('',url(r'^(?P<exp>[ASG])$', ScholarshipDirectoryView.as_view(), name='scholarship_directory'),)

(如果/A 或/S 或/G 未通过,以上将返回 404)

现在,在 CBV 的 kwargs 属性中,我们将有一个名为 exp 的 kwarg,get_queryset 方法可以使用它来过滤经验水平.

<前>类奖学金目录视图(列表视图):模型 = 奖学金template_name = '奖学金目录.html'def get_queryset(self):qs = super(ScholarshipDirectoryView, self).get_queryset()返回 qs.filter(experience_level__exact=self.kwargs['exp'])

I've read the official documentation on dynamically filtering ListView, but am still confused about how to actually use it.

I currently have a simple model, let's call it Scholarship:

class Scholarship(models.Model):
    title = models.CharField(max_length=255)
    submitted_date = models.DateField(auto_now=True, verbose_name='Date Submitted')
    EXPERIENCE_LEVEL_CHOICES = (
        ('A', 'Any'),
        ('S', 'Student'),
        ('G', 'Graduate')
    )
    experience_level = models.CharField(max_length=1, choices=EXPERIENCE_LEVEL_CHOICES, default='A')

I have a page where I'm showing all of these scholarships, using ListView:

views.py

from django.views.generic import ListView
from .models import Scholarship


class ScholarshipDirectoryView(ListView):
    model = Scholarship
    template_name = 'scholarship-directory.html'

urls.py

from django.conf.urls import patterns, url

from .views import ScholarshipDirectoryView

urlpatterns = patterns('',
    url(r'^$', ScholarshipDirectoryView.as_view(), name='scholarship_directory'),
)

I'm trying to generate links on the home page of the site that will return filtered versions of this ListView. For example, if someone clicks on a "show scholarships for graduate students" link, only scholarships with experience_level='G' will be shown.

I have no problem returning this queryset via the shell -> Scholarship.objects.filter(experience_level__exact='G')

I'm just unsure about how to dynamically filter the ListView via a dropdown or URL. Not looking to use a plugin, but rather understand how dynamically querying/filtering works in Django.

解决方案

First of all you need to change your urls.py so that it'll pass the experience as a parameter. Something like this:

urlpatterns = patterns('',
    url(r'^(?P<exp>[ASG])$', ScholarshipDirectoryView.as_view(), name='scholarship_directory'),
)

(the above will return 404 if /A or /S or /G is not passed)

Now, in kwargs attribute of the CBV we will have a kwarg named exp which can be used by the get_queryset method to filter by experience level.

class ScholarshipDirectoryView(ListView):
    model = Scholarship
    template_name = 'scholarship-directory.html'

    def get_queryset(self):
        qs = super(ScholarshipDirectoryView, self).get_queryset()
        return qs.filter(experience_level__exact=self.kwargs['exp'])

这篇关于在 Django 1.7 中动态过滤 ListView CBV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆