TemplateView helps us create Class Based Views (CBV).
Let's learn TemplateView with an example. We begin by creating view for index.html page.
Under views.py file in your app (Create one if you don't have), add the following:
from django.views.generic import View, TemplateView
class IndexView(TemplateView):
template_name = 'mainApp/index.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['text'] = "Hello World!"
context['number'] = 100
return context
Display these values from views.py in index.html in the following manner:
<h2>{{ text }} {{ number|add:"99" }}</h2>
This is a short lesson but a useful one.
Comments
Post a Comment