Hi guys,

I know, this is a quite old thread, but recently I had the same issue with 
these decorators for the same reason: how to handle 405 responses in user 
friendly way?

I have found a simple solution which doesn't require to write your own 
middleware and I want to share it with people facing the same problem in 
the future :).

The solution is: another decorator, that wraps the require_POST decorator. 
(Google for decorator chaining, if it's alien to you.)

---------------
# the view function
@handle405 # your own decorator, see the code below
@require_POST
def myView(request):
    # ... doing fancy stuff ...
    return HttpResponse("Everything is ok, it was a POST request.")

# the trick: wrap the require_POST decorator by writing your own
def handle405(func):
def checkResponse(param):

# run the function that was modified by require_POST, param=request
ret = func(param)

if ret.status_code == 405:
return HttpResponse("Sorry, it was not a POST request.")

return ret

return checkResponse

---------------

Cheers!
Zsolt


On Monday, July 8, 2013 11:01:11 AM UTC+2, Jorge C. Leitão wrote:
>
> Django allows 
> <https://docs.djangoproject.com/en/1.5/topics/http/views/#customizing-error-views>
>  
> users to define handlers to some exceptions, most notably http404, server 
> error (status 500) and permission denied (status 403).
>
> I here propose this feature to be extended to HttpResponseNotAllowed 
> <https://docs.djangoproject.com/en/1.5/ref/request-response/#django.http.HttpResponseNotAllowed>
>  (status 
> 405).
>
> There main reason is consistency: if django has a way to create such 
> status, it should also allow the developer to handle them.
>
> For instance, I was planning to write handlers for some status I raise in 
> views (mainly for debugging during live), and I'm not being able to use the 
> decorator require_http_methods 
> <https://docs.djangoproject.com/en/1.5/topics/http/decorators/#django.views.decorators.http.require_http_methods>
>  because 
> it emits a status that I cannot handle by myself. Thus, I cannot properly 
> judge if it was triggered by a server semantic error or by a "curious" user.
>
> Regards,
> Jorge
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/b509cb54-552d-4c8b-93a7-fdf4d18a9aec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to