How to get Django url correctly?
I have set up a url mapping that goes like this:
(r'enroll/$', 'enroll')
In my development envi开发者_StackOverflow社区ronment this mapping is used when I visit '/enroll/'.
But in the production environment, the Django application is under '/activity/' and '/activity/enroll/' should be used.
Please tell me how do I get the correct url in both cases.
Thanks in advance.
I would suggest doing whatever you can to get your prod and dev as identical as possible, however if thats not possible you could use separate urlpatterns for the development environment.
Assuming you have a settings.DEBUG set, try the following:
extra_patterns = patterns('',
(r'enroll/$', 'enroll'),
)
if settings.DEBUG:
urlpatterns = patterns('', (r'^', include(extra_patterns)))
else:
urlpatterns = patterns('', (r'^activity/', include(extra_patterns)))
django.core.urlresolvers.reverse()
or {% url %}
can be used to turn a view reference or named urlconf into a URL suitable for output.
精彩评论