Class based generic view: ListView in Django 1.3

Category: /blog /django
Tags: django

Resources

Default naming

  • default template called <app name>/<model name>_list.html, 可以修改 template_name来指定不同的文件
  • default context variable is object_list, 可以通过修改context_object_name来取更有意义的名字

演示代码

In urls.py

from django.conf.urls.defaults import *
from django.contrib.auth.decorators import login_required
from django.views.generic import DetailView, ListView
from polls.models import Poll

urlpatterns = patterns('',
    (r'^$',
        login_required(ListView.as_view(
            queryset=Poll.objects.order_by('-pub_date')[:5],
            context_object_name='latest_poll_list',
            template_name='polls/index.html')),
    )
)

in views.py, you can get parameters from URL, do filtering, and add to context to templates.

from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import ListView
from books.models import Book

urlpatterns = patterns('',
    (r'^books/(\w+)/$', PublisherBookListView.as_view()),
)

class AcmeBookListView(ListView):

    context_object_name = "book_list"
    template_name = "books/acme_list.html"

    def get_queryset(self):
        publisher = get_object_or_404(Publisher, 
            name__iexact=self.args[0])
        return Book.objects.filter(publisher=publisher)

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(PublisherBookListView, self)
            .get_context_data(**kwargs)
        # Add in the publisher
        context['publisher'] = self.publisher
        return context

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(ProtectedView, self).dispatch(*args, **kwargs)

Related

讨论

提示

  • 如果看不到讨论部分, 请暂时关掉adblock in Firefox/Chrome
  • 本网站使用Javascript实现评论功能, 此处外链对提高您的网站PR没有帮助. (潜台词: 请不要灌水, 谢谢)