Django TemplateView GET|POST Example — URLs, as_view and get_context_data

Django TemplateView GET|POST Example — URLs, as_view and get_context_data

In this post we will give you information about Django TemplateView GET|POST Example — URLs, as_view and get_context_data. Hear we will give you detail about Django TemplateView GET|POST Example — URLs, as_view and get_context_dataAnd how to use it also give you demo for it if it is necessary.

Django Templates are used to create HTML interfaces that get rendered with a Django view.

A TemplateView is a generic class-based view that helps developers create a view for a specific template without re-inventing the wheel.

TemplateView is the simplest one of many generic views provided by Django.

You can create a view for an example index.html template by simply sub-classing TemplateView and providing the template name via a template_name variable.

TemplateView is more convenient when you need to create views that display static HTML pages without context or forms that respond to GET requests.

TemplateView is simply a sub-class of the View class with some repetitive and boilerplate code that renders a Django template and sends it to the client.

Django View Example

Before looking at how to use TemplateView, let’s first look at how we can create a Django view from scratch.

Let’s pretend we need to create a home view. This is the required code that you need to write in the views.py file of your application

fromdjango.shortcutsimportrenderfromdjango.views.generic.baseimportViewclassHome(View):defget(self,request,*args,**kwargs):returnrender(request,"index.html")

If our app is named myapp, you need to create a templates/myapp inside myapp and then add an index.html template inside of it. The path of the index.html file should be myapp/templates/myapp/index.html.

So what does View do for us? It simply provides the get method which needs to contain any code that will be called when a GET request is sent to the associated URL.

You don’t have to check for the GET request, just provide your code inside the get method.

As you can see in this example, we used extra code to render and return the index.html template using an HttpResponse.

Django TemplateView Example

Here comes the role of TemplateView. Instead of extending View, override the get method and then process the template and return an HttpResponse object using a render function. Simpy extend TemplateView.

This is the previous example rewritten to use TemplateView:

fromdjango.views.generic.baseimportTemplateViewclassHome(TemplateView):template_name='index.html'

Next, put your index.html template in the corresponding folder and you are good to go!

You don’t need to override the get method and provide an implementation using render or another method. It’s already done for you in TemplateView.

If you look at the implementation of TemplateView, you’ll find an implementation of the get that uses the template in the template_name variable and renders it.

Since this is a common pattern, it’s easily isolated and defined in its own class which can be re-used by Django developers without re-inventing the wheel.

The only requirement is that you have to use the template_name variable for specifying the template since this is the only way TemplateView can use to recognize the template you want to render.

Django Template Context with View

If you don’t want your template to be completely static, you need to use Template Context.

Let’s see how you can provide context to your template using a View class. This is the previous example with a simple context object that we’ll be passed to the template to make it more dynamic.

fromdjango.shortcutsimportrenderfromdjango.views.generic.baseimportViewclassHome(View):defget(self,request,*args,**kwargs):context={'message':'Hello Django!'}returnrender(request,"index.html",context=context)

You simply create a context object (You can name it whatever you want) and you pass it as the second parameter of the render method.

Django Template Context with TemplateView

Let’s now see the previous example with TemplateView:

fromdjango.views.generic.baseimportTemplateViewclassHome(TemplateView):template_name='index.html'defget_context_data(self,*args,**kwargs):context=super(Home.self).get_context_data(*args,**kwargs)context['message']='Hello World!'returncontext

If using TemplateView, you need to use the get_context_data method to provide any context data variables to your template.

You re-define the get_context_data method and provide an implementation which simply gets a context dict object from the parent class (in this example it’s TemplateView) then augments it by passing the message data.

You can then use interpolation curly braces in your index.html template to display your context variable

{{message}}</p>

Using TemplateView with URLs with as_view

After defining the sub-class of TemplateView you need to map it with an URL in the urls.py file of your project. In order to do that, you need to call the as_view method of TemplateView which returns a callable object that can be passed as the second parameter for the path method that associates URLs to views. For example:

fromdjango.urlsimportpathfrommyappimportviewsurlpatterns=[# [...]path('',views.Home.as_view())]

Using TemplateView in urls.py

For even simpler cases, you can use TemplateView directly in your URL. This provides you with a quicker way to render a template:

fromdjango.views.generic.baseimportTemplateViewfromdjango.urlsimportpathurlpatterns=[# [...]path('',TemplateView.as_view(template_name='index.html'))]

In the as_view method of TemplateView, you can pass the template name as a keyword argument.

Conclusion

The Django generic TemplateView view class enables developers to quickly create views that display simple templates without reinventing the wheel.

You simpy need to subclass TemplateView and provide a template name in the template_name variable. This template should obviously exist in your templates folder.


Hope this code and post will helped you for implement Django TemplateView GET|POST Example — URLs, as_view and get_context_data. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US