Last modified: Jan 10, 2023 By Alexander Williams
Django TemplateView [Simple Example]
in this article we'll explore how to use templateView in django.
when you should use TemplateView
TemplateView should be used when you want to present some information in a html page. TemplateView shouldn't be used when your page has forms and does creation or update of objects. In such cases FormView, CreateView or UpdateView is a better option.
example
# app/views.py
from django.views.generic import TemplateView
class IndexView(TemplateView):
template_name = "index.html" #your_template
#urls.py
from django.urls import path
from some_app.views import IndexView
urlpatterns = [
path('index/', IndexView.as_view()),
]
1 2 3 4 5 6 7 8 9 10 11 | <!-- index.html --> <html> <head> <title>index</title> </head> <body> <h1>Hello Django TemplateView</h1> </body> </html> |
all done!