在Django 1.7中动态过滤ListView CBV [英] Dynamically filter ListView CBV in Django 1.7
问题描述
我已阅读关于动态过滤的ListView的官方文档,但仍然对如何实际使用它感到困惑。
我目前有一个简单的模型,我们称之为奖学金
:
pre>
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')
我有一个页面显示所有这些奖学金,使用ListView:
视图。 py
from django.views.generic import ListView
from .models import奖学金
class ScholarshipDirectoryView(ListView):
model =奖学金
template_name ='scholarship-directory.html'
urls.py
从django.conf.urls导入模式, url
from .views import ScholarshipDirectoryView
urlpatterns = patterns('',
url(r'^ $',ScholarshipDirectoryView.as_view(),name ='奖学金目录'),
)
我试图在主页上生成链接该网站将返回此ListView的过滤版本。例如,如果有人点击为研究生颁发奖学金链接,那么只会显示 experience_level ='G'
的奖学金。
我没有问题通过shell返回此查询器 - > Scholarship.objects.filter(experience_level__exact ='G')
我只是不确定如何通过下拉列表或URL动态过滤ListView。不想使用插件,而是了解动态查询/过滤在Django中的工作原理。
首先,您需要更改urls.py,以便将经验作为参数传递。这样的事情:
urlpatterns = patterns('',
url(r'^(?P< exp> [ASG ])$',ScholarshipDirectoryView.as_view(),name ='scholarship_directory'),
)
(以上将返回404 if / A或/ S或/ G未通过)
现在,在CBV的 kwargs
属性中我们将有一个名为 exp
的kwarg,可以由 get_queryset
方法用于按经验级别进行过滤。
class ScholarshipDirectoryView(ListView):
model =奖学金
template_name ='scholarship-directory.html'
def get_queryset(self):
qs = super(ScholarshipDirectoryView,self).get_queryset()
return 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屋!