Domanda

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 reg ex 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/*/)

#in ir_site.urls
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)
)

#in en_site.urls
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 much appreciated...

È stato utile?

Soluzione 2

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.

Altri suggerimenti

Change your urls.py to the following for the includes. You need to have a trailing / on the url.

url(r'^fa/', include('ir_site.urls')),
url(r'^en/', include('en_site.urls')),

Also for your url parameter, I think you'd be better off using a + rather than an * since it will require 1 or more characters whereas the * is 0 or more.

Finally, in ir_site.urls and en_site.urls you should lead the contact with with an ^ so it will only match when contact is the first part of the url (after en/ or fa/).

Edit: Change the flatpage url pattern. Currently using the . will match a /. The example I've giving will match any alphanumeric character long with hyphens.

(r'^(?P<url>[-\w]+)/$', 'flatpage'),
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top