4

I'm using Django 1.1.1 stable. When DEBUG is set to True Django flatpages works correctly; when DEBUG is False every flatpage I try to access raises a custom 404 error (my error template is obviously working correctly).

Searching around on the internet suggests creating 404 and 500 templates which I have done.

I've added to FlatpageFallBackMiddleware to middleware_classes and flatpages is added to installed applications. Any ideas how I can make flatpages work?

Aaron Butacov
  • 32,415
  • 8
  • 47
  • 61
Adam
  • 41
  • 2

5 Answers5

6

The same happened to me until I found that the 404 view was sending a 200 status response. So all you have to do is add this in the view that handles your 404 response:

def 404_handler(request):    ...

    response = render_to_response('404.html', locals(), context_instance=RequestContext(request))
    response.status_code = 404
    return response
Javi Romero
  • 327
  • 5
  • 14
1

try to add FlatpageFallBackMiddleware before django.middleware.common.CommonMiddleware

and be sure, that your 404.html and 500.html are stored in the root of your templates dir (eg: templates/404.html)

bmaeser
  • 982
  • 7
  • 13
0

Make an error handling view that prints a stacktrace import traceback;traceback.print_exc() instead of ignoring the error silently.

Druska
  • 4,752
  • 2
  • 30
  • 34
0

The key is checking the order of your middleware. Middleware is executed in top-down order on the way in (request and view) and in bottom-out order on the way out (response and exception). So if you are getting to your 404 handler on what should be a perfectly reasonable flatpage URL then something is catching the 404 before the flatpages middleware is getting called.

Peter Rowell
  • 17,605
  • 2
  • 49
  • 65
0

Had the same error in a different context. The problem was caused by me changing file urls.py from

from django.conf.urls.defaults import *

to

from django.conf.urls.defaults import include, patterns

as suggested by pylint, but this omits handler404 and handler500 which are expected to be imported implicitly by import *.

so either adding those to import or just importing * as django documents suggest solved the issue.

Evgeny
  • 10,698
  • 9
  • 60
  • 70