0

I have some redirects for URLs that are no longer valid so I need them to return a 410. Because the URLs are from an older version of the website (with a completely different system) I simply put them in the redirects app for easier editing. However a 410 "redirect" doesn't return anything else than the status code - the page normally shown is just empty.

How can I display something meaningful to the user (like "page no longer exists")? It would be nice to have some kind of template similar to the template for 404 errors.

Any ideas?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Semmel
  • 2,526
  • 3
  • 21
  • 30

1 Answers1

0

Just change the status code on the response to be 410.

from django.shortcuts import render

def my_view(request):
    context = {}
    return render(request, '410.html', context, status=410)

Docs on render shortcut: https://docs.djangoproject.com/en/1.3/topics/http/shortcuts/#render

You could also create an exception Http410 and a middleware to catch this exception to render a 410 page. This is how 404 pages are often handled in Django.

Mark Lavin
  • 24,664
  • 5
  • 76
  • 70
  • I'm just using regular redirects so rendering my own is not an option. The second option sounds better. I also thought about some kind of middleware - would it be enough if it would just catch the normal 410 errors? Or do I have to write my own exception (which - again - is not an option). – Semmel May 04 '12 at 14:36
  • See @dm03514's comment for a middleware/exception example. I'm not sure I understand what you mean by "regular" redirects. Somewhere you must determine that the link is not valid and that place should return the 410. – Mark Lavin May 04 '12 at 14:49
  • Sorry - I should have been more concise. I'm using the redirects app from Django and when I don't do a redirect there I do get a 410 but without any further notice. I mean - shouldn't I get some kind of information (just like the 404 page) instead of a blank page? – Semmel Jun 13 '12 at 15:21