Django Set HTTP Error manually

1,120 views
Skip to first unread message

johnny

unread,
Apr 9, 2007, 10:39:21 PM4/9/07
to Django users
What I want to do is set HTTP Error manually in my view, when a
request is made to certain url, without post data.

At a particular url, my view is looking for XML document to be sent
over http, by post. If a request come in without post, I want to
raise an error "405 Method Not Allowed". How do I do this?

Thank you

James Bennett

unread,
Apr 9, 2007, 10:41:49 PM4/9/07
to django...@googlegroups.com
On 4/9/07, johnny <ramp...@gmail.com> wrote:
> At a particular url, my view is looking for XML document to be sent
> over http, by post. If a request come in without post, I want to
> raise an error "405 Method Not Allowed". How do I do this?

response = HttpResponse('some output here')
response.status_code = 405
return response

--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Ivan Sagalaev

unread,
Apr 10, 2007, 2:02:56 AM4/10/07
to django...@googlegroups.com

In fact 405 doesn't mean "post without data", you should answer 405 if
you receive anything except 'POST' in HTTP method. This is done by a not
very well-known decorator:

from django.views.decorators.http import require_http_methods

@require_http_methods('POST')
def your_view(request):
...

I think your app should react on an empty POST as it does on any invalid
XML document. I mean just get request.raw_post_data and send it to a
parser as you (presumably) do now. It doesn't matter if it has '' or
'some garbage' there.

Jesse Lovelace

unread,
Apr 11, 2007, 5:31:16 PM4/11/07
to django...@googlegroups.com
Or:

from django.http import HttpResponseNotAllowed

return HttpResponseNotAllowed()

Reply all
Reply to author
Forward
0 new messages