Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which to use in Django: ListView or list_detail?

I was reading some tutorials and books about generic views.

In part 4 of the official tutorial, they wrote an example like this

from django.conf.urls import patterns, include, url
from django.views.generic import DetailView, ListView
from polls.models import Poll

urlpatterns = patterns('',
    url(r'^$',
        ListView.as_view(
            queryset=Poll.objects.order_by('-pub_date')[:5],
            context_object_name='latest_poll_list',
            template_name='polls/index.html')),
    url(r'^(?P<pk>\d+)/$',
        DetailView.as_view(
            model=Poll,
            template_name='polls/detail.html')),
    url(r'^(?P<pk>\d+)/results/$',
        DetailView.as_view(
            model=Poll,
            template_name='polls/results.html'),
        name='poll_results'),
    url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
)

I have also been reading The Definitive Guide to Django: Web Development Done Right, Second Edition and when they talked about generic views, they wrote their example like this

from django.conf.urls.defaults import *
from django.views.generic import list_detail
from mysite.books.models import Publisher

publisher_info = {
    'queryset': Publisher.objects.all(),
    'template_name': 'publisher_list_page.html',
}

urlpatterns = patterns('',
    (r'^publishers/$', list_detail.object_list, publisher_info)
)

Should I be using ListView or list_detail? They both come from django.views.generic. If they can both be used, then what's the difference (advantage and disadvantage comparison)?

In case it helps, I'll explain my objective: In my project, I want to list work orders, and then I want a detailed view of each work order that will also contain a list of comments for that order (similar to comments per blog post).

like image 709
hobbes3 Avatar asked Mar 23 '12 04:03

hobbes3


People also ask

What is the use of ListView in Django?

ListView. A page representing a list of objects. While this view is executing, self. object_list will contain the list of objects (usually, but not necessarily a queryset) that the view is operating upon.

How can I see views in Django?

To get all the views present in a Django project, we create a function get_all_view_names() which takes urlpatterns as input and returns the complete list of views being used in the project as the output. First, we import the root_urlconf module using settings. ROOT_URLCONF . Then root_urlconf.

What is the difference between a list view and detail view in Django?

The ListView approach seems more semantic, because what I want is a list of posts, but it's also slightly more complex. It requires that I overwrite two methods. The DetailView approach only requires me to overwrite one method.

Why we use generic views in Django?

Django's generic views were developed to ease that pain. They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to write too much code.


1 Answers

I'm finding Classy useful as an easy way to view an outline of each CBV: http://ccbv.co.uk/projects/Django/1.6/django.views.generic.list/ListView/

It feels like a missing part of the Django docs now.

like image 51
trojjer Avatar answered Oct 08 '22 21:10

trojjer