Reusable Views
The best way to build views quickly in Django is simply use view that someone else has implemented and debugged.
Generic Views
TemplateView
From Class-based Views Base Views
Template in urls.py
urls.py
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('about/', TemplateView.as_view(template_name="about.html")),
]
Subclass TemplateView
urls.py
from django.urls import path
from some_app.views import AboutView
urlpatterns = [
path('about/', AboutView.as_view()),
]
views.py
from django.views.generic import TemplateView
class AboutView(TemplateView):
template_name = "about.html"
templates/about.html
<h1>Shrinking World</h1>
<p>
Founded in 2007 to bring practical software development training to
students everywhere.
</p>
Setting Data in Views
urls.py
from django.urls import path
from .views import MyStoryView
urlpatterns = [
path('story', MyStoryView.as_view()),
]
views.py
from django.views.generic import TemplateView
class MyStoryView(TemplateView):
template_name = "story.html"
def get_context_data(self, **kwargs):
return {
'title': 'Three Pigs',
'body': 'Once upon a time ...',
}
templates/story.html
<h1>{{ title }}</h1>
<p>
{{ body}}
</p>
RedirectView
urls.py
from django.urls import path
from .views import DirectoryIndexView, RandomRedirectView
urlpatterns = [
path('directory/', DirectoryIndexView.as_view()),
path('random', RandomRedirectView.as_view()),
]
views.py
from django.views.generic import RedirectView
class DirectoryIndexView(RedirectView):
url = "directory/index.html"
class RandomRedirectView(RedirectView):
def get_redirect_url(self, *args, **kwargs):
article = choice ([
'http://google.com',
'http://amazon.com',
'http://facebook.com',
])
return article