views:

308

answers:

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?

A: 

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
+1  A: 

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)

renton
+1  A: 

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
Gooler
A: 

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