Entries published on December 11, 2007
Requiring HTTP methods
Just a quick tip today: someone on IRC tonight was asking for an easy way to write a Django view which restricts itself to only allowing a specific HTTP method or methods. For example, a web-based API might want to only allow POST
to specific views.
This is actually pretty easy to do with a set of decorators built in to Django, in the module django.views.decorators.http
. Specifically, the fix for ticket #703 added three useful things to that module:
-
require_POST
is a decorator which checks that the HTTP method of the request wasPOST
, and returns HTTP 405 — “method …