English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Django Generic Views

In some cases, writing view code can be really cumbersome, as we have seen before. Imagine that you just need a static page or a list page. Django also provides a simple way to set these simple views, called generic views.

Different from traditional views, a generic view is a class without functions. Django also provides a set of class django.views.generic generic views, and each regular view is a class or inherits from one of these classes.

There are10+Generic class −

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
>>> import django.views.generic
 >>> dir(django.views.generic)
 ['ArchiveIndexView', 'CreateView', 'DateDetailView', 'DayArchiveView', 
    'DeleteView', 'DetailView', 'FormView', 'GenericViewError', 'ListView', 
    'MonthArchiveView', 'RedirectView', 'TemplateView', 'TodayArchiveView', 
    'UpdateView', 'View', 'WeekArchiveView', 'YearArchiveView', '__builtins__', 
    '__doc__', '__file__', '__name__', '__package__', '__path__', 'base', 'dates', 
    'detail', 'edit', 'list'

You can use generic views. Let's look at some examples to see how it works.

Static web pages

Let's publish the static page from the 'static.html' template.

Our static.html −

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
<html>
    <body> 
       This is a static page!!! 
    </body>
 </html>

If we do this, in the way we learned before, we will have to change myapp/views.py −

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
from django.shortcuts import render
 def static(request):
    return render(request, 'static.html', {})

myapp/urls.py will be as follows -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
from django.conf.urls import patterns, url
 urlpatterns = patterns("myapp.views", url(r'^static/','static', name='static'),)

The best way is to use generic views. For this, our myapp/views.py will become -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
from django.views.generic import TemplateView
 class StaticView(TemplateView):
    template_name = "static.html"

And our myapp/urls.py will be as follows -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
from myapp.views import StaticView
 from django.conf.urls import patterns
 urlpatterns = patterns("myapp.views", url(r'^static/, StaticView.as_view()),)    , StaticView.as_view()),)

When accessing /myapp/static will get -

For the same result, we can also, perform the following operation -

No need to modify views.py        Change the url.py file to -    

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
from django.views.generic import TemplateView
 from django.conf.urls import patterns, url
 urlpatterns = patterns("myapp.views",
    url(r'^static/','TemplateView.as_view(template_name='static.html')),)

As you can see, you only need to change the second method in the url.py file.

Listing and displaying data from the database

We need to list all entries in the Dreamreal model. This makes it easy to use the ListView generic view class. Edit the url.py file and update it -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
from django.views.generic import ListView
 from django.conf.urls import patterns, url
 urlpatterns = patterns(
    "myapp.views", url(r'^dreamreals/','ListView.as_view(model=Dreamreal, 
       template_name = "dreamreal_list.html")),
 )

It is important to note that at this point, the variable is passed from the generic view to the template as object_list. If you want to use your own name, you will need to add a context_object_name parameter to the as_view method. Then, url.py becomes -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
from django.views.generic import ListView
 from django.conf.urls import patterns, url
 urlpatterns = patterns("myapp.views",
    url(r'^dreamreals/', ListView.as_view(
       template_name = "dreamreal_list.html")),
       model = Dreamreal, context_object_name = "dreamreals_objects",)

Then the associated template will become −

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
{% extends "main_template.html" %}
 {% block content %}
 Dreamreals:<p>
 {% for dr in object_list %}
 {{ dr.name }}</p>
 {% endfor %}
 {% endblock %}

Access /myapp/dreamreals/ The following page will be generated −