I'm having real troubles understanding how url routing works in django when using flatpages and was hoping someone could take a look at my code and explain what I'm doing wrong.
Also, it would be really helpful to have some advice on debugging this - ideally, when a request is successfully resolved, I'd like to see how django reaches that resolution. At the moment I only see the url patterns attempted when I get a 404.
Thanks in advance,
In settings.py
I have APPEND_SLASH = True
.
I have the following URL structure, in brackets I have indicated what I think my regex should be doing..
urls.py
:
url(r'^$', home), # (match / and nothing else)
url(r'^fa/$', home), # (match /fa and nothing else)
url(r'^en/$', en_home), # (match /en and nothing else)
url(r'^fa', include('ir_site.urls')), # (match any path like fa/*/)
url(r'^en', include('en_site.urls')), # (match any path like en/*/)
ir_site/urls.py
:
urlpatterns = patterns('',
url(r'contact/$', views.static_contact) # (match fa/contact)
)
urlpatterns += patterns('django.contrib.flatpages.views',
(r'^(?P<url>.*/)$', 'flatpage'), # (match any fa/*/ not resolved or 404)
)
en_site/urls.py
:
urlpatterns = patterns('',
url(r'contact/$', views.static_contact) # (match en/contact)
)
urlpatterns += patterns('django.contrib.flatpages.views',
(r'^(?P<url>.*/)$', 'flatpage'), # (match any en/*/ not resolved or 404)
)
I have the following flat pages
/en/test/
/fa/test/
/test/
/en/test2
Results of testing:
/ - Fine
/en - Fine
/fa - Fine
/test - Fine
/fa/test - Fine
/en/test - Extra / is appended, results in 404
/en/test2 - Fine, but uses the flatpages template in the `ir_site` app
/en/contact - Fine
/fa/contact - Fine
I have a feeling the problem is with r'^fa'
and r'^en'
not working correctly with the catch all for flatpages but I don't know how to see why that is as I lack any kind of debug skills at this stage.
Help is much appreciated...
So by removing any reference to flatpages in urls.py and just using the middleware, I found all problems disappeared and everything started to work as expected.