[Django]-How do I use POST in django1.5 with TemplateView?

4👍

There is a TemplateResponse response class which takes the same parameters as direct_to_template shortcut. You can just replace and be safe

return TemplateResponse(request, template)

3👍

The render shortcut can be used as a replacement for direct_to_template for this usage. It takes the same parameters as direct_to_template so it can be a simple find and replace.

return direct_to_template(request, template)

becomes

return render(request, template)

2👍

In general you don’t have to explicitly return a TemplateView like that, you just put it in your urls and you’re done with it.

Now, the reason you don’t get a response with a Post is that TemplateView only defines a GET method

So you would need to create a new class that inherits from TemplateView in order to implement a POST method yourself.

My recommendation would be to look for the other CBV and see if one matches your needs better, perhaps a FormView would suffice

https://github.com/django/django/blob/master/django/views/generic/base.py#L147

Leave a comment