Sunday, September 23, 2018

Re: what's the difference between TemplateView, ListView and DetailView ?

Django provides base view classes which will suit a wide range of applications. All views inherit from the View class, which handles linking the view in to the URLs, HTTP method dispatching and other simple features. RedirectView is for a simple HTTP redirect, and TemplateView extends the base class to make it also render a template.

TemplateView (docs)
Renders a given template, with the context containing parameters captured in the URL.
Use it when you want just to render some template, without getting objects from database. 

ListView (docs)
A page representing a list of objects.
Use it when you want to represent multiple objects from your database on a list (single template with multiple objects -- e.g. a list of products that are available in the shop or list of to-dos...).

DetailView (docs)
While this view is executing, self.object will contain the object that the view is operating upon.
Use it when you want to represent single object from your database (single template with one object, detailed -- e.g. product page).

Why to use DetailView and ListView instead of always using TemplateView?
Main reason is that using the views simplifies writing and reading the code. You can use built-in methods that do almost the whole job for you (actually they are used "automagically" and if you want them to do something else, e.g. add filtering to your ListView, you can just override them).


W dniu niedziela, 23 września 2018 15:47:17 UTC+2 użytkownik Gear Crew napisał:
I want to know when I use TemplateView , ListView and DetailView on my template code

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a20803dd-6590-468f-a023-c71d3ba1633b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment