Reputation: 617
Following urls are valid:
//localhost/
//localhost/123
//localhost/hello/
//localhost/hello/456
url.py
url(r'^$', views1.custom1, name='custom1'),
url(r'^(?P<param1>.+)/$', views1.custom1, name='custom1'),
url(r'^hello/$’, views2.custom2, name='custom2’),
url(r'^hello/(?P<param2>.+)/$', views2.custom2, name='custom2’),
view1.py
def custom1(request, param1=''):
view2.py
def custom2(request, param2=''):
For url //localhost/hello/, custom1() function responds, with param1='hello' which is not correct!
Following 2 url can’t be distinguished.
url(r'^(?P<param1>.+)/$', views1.custom1, name='custom1'),
url(r'^hello/$’, views2.custom2, name='custom2’),
How to fix it?
Upvotes: 0
Views: 32
Reputation: 5405
If you look at the Django docs for URLS, you'll see this:
- Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.
This means that on the first matching URL, Django will stop looking, and pass the request to the corresponding view. localhost/hello/
is a valid pattern for the regex '^(?P<param1>.+)/$'
To fix this, simply reorder you url_patterns
like this:
url(r'^$', views1.custom1, name='custom1'),
url(r'^hello/$’, views2.custom2, name='custom2’),
url(r'^hello/(?P<param2>.+)/$', views2.custom2, name='custom2’),
url(r'^(?P<param1>.+)/$', views1.custom1, name='custom1'),
Upvotes: 1