views.generic.base.TemplateView

class django.views.generic.base.TemplateView

Renders a given template, with the context containing parameters captured in the URL.

Ancestors (MRO)

This view inherits methods and attributes from the following views:

Method Flowchart

  1. dispatch()
  2. http_method_not_allowed()
  3. get_context_data()

Example views.py:

1
2
3
4
5
6
7
8
9
10
11
12
from django.views.generic.base import TemplateView
 
from articles.models import Article
 
class HomePageView(TemplateView):
 
    template_name = "home.html"
 
    def get_context_data(self, **kwargs):
        context = super(HomePageView, self).get_context_data(**kwargs)
        context['latest_articles'] = Article.objects.all()[:5]
        return context

Example urls.py:

1
2
3
4
5
6
7
from django.conf.urls import url
 
from myapp.views import HomePageView
 
urlpatterns = [
    url(r'^$', HomePageView.as_view(), name='home'),
]

Context

  • Populated (through ContextMixin) with the keyword arguments captured from the URL pattern that served the view.
doc_Django
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.