Generic Views
In some cases, it becomes difficult for us to write views as we have done in the previous examples. Suppose that you are in need of a static page. Generic views in Django provide us with a mechanism for how to set views in a simple manner. These can be seen as classes rather than as functions, which is the case with classic views. The classes for Generic views in Django can be found in the class “django.views.generic”.
To view the available generic classes, which are over 10, the following steps are necessary:
>>> 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’]
- Static Pages :-
We need to publish a static page from the template “static.html”. Here is the code for the file “static.html”:
<html>
<body>
A static page sample!!!
</body>
</html>
For this to be done as we learned before, the file “myapplication/views.py” has to be changed to the following:
from django.shortcuts import render
def static(request):
return render(request, ‘static.html’, {})
And the file “myapplication/urls.py” has to be changed to the following:
from django.conf.urls import patterns, url
upatterns = patterns(“myapplication.views”, url(r’^static/’, ‘static’, name = ‘static’),)
This can be solved effectively by use of the generic views. In this case, the file “myapplication/views.py” will be as follows:
from django.views.generic import TemplateView
class StaticView(TemplateView):
template_name = “static.html”
And the file “myapplication/urls.py” should be as follows:
from myapplication.views import StaticView
from django.conf.urls import patterns
upatterns = patterns(“myapplication.views”, (r’^static/$’, StaticView.as_view()),)
Alternatively, we can achieve this by changing the file “url.py” and making no change to the file “views.py”. This is shown below:
from django.views.generic import TemplateView
from django.conf.urls import patterns, url
upatterns = patterns(“myapplication.views”,
url(r’^static/’,TemplateView.as_view(template_name = ‘static.html’)),)
As shown in the above code, only the file “url.py” has been changed in the second method.
- Listing and Displaying Data from the DB :-
Now we need to list our entries in the Dreamreal model. The generic view class named “ListView” makes this easy for us to do. You just have to edit the file “url.py” and update it to get the following:
from django.views.generic import ListView
from django.conf.urls import patterns, url
upatterns = patterns(
“myapplication.views”, url(r’^dreamreals/’, ListView.as_view(model = Dreamreal,
template_name = ” list.html”)),
)
The file “url.py” should now become as shown below:
from django.views.generic import ListView
from django.conf.urls import patterns, url
upatterns = patterns(“myapplication.views”,
url(r’^dreamreals/’, ListView.as_view(
template_name = ” list.html”)),
model = Dreamreal, context_object_name = ”dreamreals_objects” ,)
The associated template should be as shown below:
{% extends ” template.html” %}
{% block content %}
Dreamreals:<p>
{% for d in object_list %}
{{d.name}}</p>
{% endfor %}
{% endblock %}
👈Episode 10(D). Episode 12(D)👉
Share This Post
PRINT THIS POST
No comments:
Post a Comment
If you have any doubts. Please let me know.