Django failing to route url (simple question)
I'm doing something stupid, and I'm not sure what it is. I have a the following urls.py in the root of my django project:
from django.conf.urls.defaults import *
from django.conf import settings
urlpatterns = patterns('',
(r'^$', include('preview_signup.urls')),
)
In my preview_signup module (django app) I have the following urls.py file:
from django.conf.urls.defaults import *
urlpatterns = patterns('django.views.generic.simple',
(r'^thanks/$', 'direct_to_template', {'template': 'thankyou.html'})
)
The urls.py above doesn't work when I go to http://localhost:8000/thanks/. But if it's change开发者_Python百科d to this:
from django.conf.urls.defaults import *
urlpatterns = patterns('django.views.generic.simple',
(r'^$', 'direct_to_template', {'template': 'thankyou.html'})
)
And I go to http://localhost:8000/ it works fine.
What am I doing wrong?
This code should work:
urlpatterns = patterns('',
(r'^', include('preview_signup.urls')),
)
$ (end of line) just removed.
When something goes wrong (or even if it doesn't), thoroughly read the django docs. Here's an excerpt from the aforementioned link:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^weblog/', include('django_website.apps.blog.urls.blog')),
(r'^documentation/', include('django_website.apps.docs.urls.docs')),
(r'^comments/', include('django.contrib.comments.urls')),
)
Note that the regular expressions in this example don't have a $ (end-of-string match character) but do include a trailing slash. Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
精彩评论