Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to raise a 410 error in Django

I'd like to return 410 errors at for some of my Django pages instead of returning 404s. Basically, instead of calling raise Http404('some error message'), I would like to instead call raise Http410('some error message') shortcut.

I am confused because in django.http, the function Http404 is simply:

class Http404(Exception):
    pass

So if I do the same thing and create my Http410 function, I would assume it would look like:

class Http410(Exception):
    pass

However, doing this returns the exception but serves up a 500 error page. How do I recreate the magic of the Http404 exception? I should note, I need to raise the exception from my models (not views) so I can't just return an HttpResponseGone.

Thanks in advance!

Update: I am fully aware of HttpResponseGone and mentioned this in my original question. I already know how to return this in my views. My question is: How do you raise an Http 410 exception similarly to how you raise an Http 404 exception? I want to be able to raise this exception anywhere, not just in my views. Thanks!

like image 939
Spike Avatar asked Nov 17 '10 19:11

Spike


People also ask

What is a 410 error page?

What Is a 410 Error? 4xx status codes mean that there was probably an error in the request, which prevented the server from being able to process it. Specifically, a 410 error means “gone.” In Google's terms, “the server returns this response when the requested resource has been permanently removed.


1 Answers

from django.http import HttpResponse
return HttpResponse(status=410)
like image 93
Spike Gronim Avatar answered Oct 11 '22 09:10

Spike Gronim