views:

282

answers:

1

Hi

I'm having a problem with configuring Flatpages in Satchmo. I've used them before, in a pure django app, but now it just doesn't work, returning 301 http error when I'm trying to enter a flatpage-configured site.

What I've done to configure it:

  • added the middleware "django.contrib.flatpages.middleware.FlatpageFallbackMiddleware" to MIDDLEWARE_CLASSES as last in the list,
  • configured example pages in admin module.

Simply what the docs say about flatpages config.

I'm feeling helpless. Don't know how could I debug this issue. Any thoughts on that?

And of course help appreciated.

Thanks to suggestion by Peter I've managed to narrow the problem to my urls.py file, for the satchmo shop.

The urlpatterns has only one entry:

(r'', 'django.views.generic.simple.redirect_to', {'url' : '/shop/'}),

This version does not work and moreover interfere with flatpages. But disabling flatpages from MIDDLEWARE_CLASSES and adding it to urls.py like the snippet below works:

(r'^(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage'),
(r'', 'django.views.generic.simple.redirect_to', {'url' : '/shop/'}),

However next problem is with the redirection from / to /shop/. With the above config it results in infinite loop.

Perhapse You know the reason for that behavior (redirect overriding flatpage) and maybe You could suggest some working solution to this problem or what should be done with requests to /.

+1  A: 

It returns 301? That's Page Moved Permanently (HttpResponsePermanentRedirect) and there are no references to that in the flatpages directory so I don't think it's coming from there. In fact there are only about 5 references to HttpResponsePermanentRedirect in all of the standard 1.1.1 release.

Possible approaches:

  1. Comment out the FlatPages middleware and see if the error changes (I'm betting it won't).
  2. Try changing the order of your MIDDLEWARE classes and see if things change.

When presenting a problem like this it's better to get very specific by showing the exact code from applicable portions of settings.py (or whatever) and by giving other things like precise URLs and the urls.py patterns you are trying to match.

Update:

OK, some random thoughts:

  1. The pattern (r'^(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage'), will match anything. Any patterns after it will never be seen.

  2. flatpages doesn't work by being called directly, it does its magic in middleware. It looks for 404 responses (Page Not Found) and then looks to see if that path exists in its table. If so, it calls a view that renders the page, etc. etc. If it doesn't find a match it let's the 404 continue on through middleware processing.

  3. The pattern (r'', 'django.views.generic.simple.redirect_to', {'url' : '/shop/'}), will match anything (I just tested it). If you want to match an empty path, use r('^$', etc.). This is the source of your infinite loop.

If you are new to regular expressions the Django urls.py file can seem like F*cking Magic. I recommend starting very simply and add one rule at a time. Do some quick tests to ensure that the new rule a) matches stuff you want it to match, and b) doesn't match stuff it shouldn't. In particular, make sure that some of the rules that occur later in the file are still reachable. In this case they wouldn't have been which should have raised a red flag.

Peter Rowell
I've edited my question, maybe you know the solution?
Marcin Cylke