What to use in Django: ListView or list_detail?

I read several textbooks and books about general ideas .

In part 4 of the official textbook , 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 also read Django Ultimate Guide: Web Development Done Right, Second Edition , and when they talked about general concepts, they wrote their example as follows

 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 use ListView or list_detail ? Both come from django.views.generic . If both of them can be used, then what is the difference (advantage and disadvantage of comparison)?

In case this helps, I will explain my purpose: in my project I want to list the work orders, and then I want to get a detailed view of each work order, which will also have a list of comments for this order (similar to the comments on the message in blog).

+6
source share
2 answers

ListView (based on a class) is intended to replace object_list (based on functions), since there is limited flexibility to extend the behavior of a function.

As notes from the Django documentation (1.4) , generic function-based views are deprecated in favor of class-based versions. So use ListView since Django removed object_list .

In any case, I prefer to put all the settings in views.py to avoid cluttering urls.py, which is usually a dump of things.

+10
source

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

Now it feels like the missing piece of Django docs.

+12
source

Source: https://habr.com/ru/post/911471/


All Articles