Dynamically filter ListView CBV in Django 1.7

半腔热情 提交于 2019-11-29 12:37:37

问题


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.


回答1:


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'])


来源:https://stackoverflow.com/questions/25662374/dynamically-filter-listview-cbv-in-django-1-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!