Django Generic Views - Django Views Tutorial | W3school

View writing is a bit difficult task in a few of the cases. For the successful completion of such a task, you will be required to have a static or a listing page. Django offers an easy way to set simple views which are also known as generic views.

Also Read: Django Template System

Generic views are different from classic views as they are not functions but classes. Django offers a set of classes which can be used for generic views and this set of classes fall under django.views.generic. A generic view is either one of the classes from the set or a class that inherits the properties of the offered set of classes for generic views.

Here are a few generic classes which are given below:

>>> 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’]

[post_middile_section_ad]

All of these classes can be used for generic views. The below section deals with how this generic view works.

Static Pages

In here, we will be publishing a static page by using the static.html template.

static.html

<html>

  <body>

     This is a static page!!!

  </body>

</html>

Now, if you want this to stay the way we did it before then the content of the myapp/views.py file will appear as the following:

from django.shortcuts import render

def static(request):

  return render(request, ‘static.html’, {})

And the content of the myapp/urls.py file will appear as:

from django.conf.urls import patterns, url

urlpatterns = patterns(“myapp.views”, url(r’^static/’, ‘static’, name = ‘static’),)

But we would recommend you to do it by using generic views. In that case, the myapp/views.py file will have the following content:

from django.views.generic import TemplateView

class StaticView(TemplateView):

  template_name = “static.html”

And the myapp/urls.py file will appear as:

from myapp.views import StaticView

from django.conf.urls import patterns

urlpatterns = patterns(“myapp.views”, (r’^static/$’, StaticView.as_view()),)

[post_middile_section_ad]

Now, when you will access the /myapp/static by entering http://127.0.01:8000/myapp/static/ the screen will display the given output:

static page

There are other ways through which we can achieve the same result. You just need to

* let the views.py file remain untouched

* alter the content of the url.py file as shown below:

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’)),)

Listing and Displaying Data from The Database (Dynamic Pages)

Here, we will re-considering our Dreamreal model. And by using that model, we will be listing out all the entries fed to the model. For this purpose, the easy way to do is to use the ListView generic view class. Firstly, you are required to make a few changes in the url.py file as shown below:

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”)),

)

[post_middile_section_ad]

You must have noticed that the variable which is passed to the template by the generic view is object_list. In case, you want to name this variable as per your convenience then you need to add a context_object_name argument to the as_view method. After which, the url.py file should have similar content to the:

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” ,)

The corresponding template will appear as:

{% extends “main_template.html” %}

{% block content %}

Dreamreals:<p>

{% for dr in object_list %}

{{dr.name}}</p>

{% endfor %}

{% endblock %}

Finally, when you enter the URL – http://127.0.0.1:8000/myapp/dreamreals/ the screen will appear as the following:

list display from db