Larry Martell
Larry Martell

Reputation: 3756

How do I use POST in django1.5 with TemplateView?

I am trying to port my app from django 1.4 to 1.5. I changed all my occurrences of:

return direct_to_template(request, template)

to:

return TemplateView.as_view(template_name=template)(request)

This works fine for all my forms that use GET, but for my forms that use POST I end up with a blank page on my browser. No error anywhere, just a blank page.

In 1.5 what do I use in place of direct_to_template for a POST?

Upvotes: 3

Views: 2505

Answers (3)

Dmitry Demidenko
Dmitry Demidenko

Reputation: 3407

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)

Upvotes: 4

Mark Lavin
Mark Lavin

Reputation: 25164

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)

Upvotes: 3

armonge
armonge

Reputation: 3138

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

Upvotes: 2

Related Questions