satoru
satoru

Reputation: 33235

How to get Django url correctly?

I have set up a url mapping that goes like this:

(r'enroll/$', 'enroll')

In my development environment 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.

Upvotes: 3

Views: 198

Answers (2)

Robert Clark
Robert Clark

Reputation: 488

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)))

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799490

django.core.urlresolvers.reverse() or {% url %} can be used to turn a view reference or named urlconf into a URL suitable for output.

Upvotes: 2

Related Questions