1

I have two patterns like this...

url(r'^(?P<page_lang>\w+)/(?P<page_title>\w+)/$', 'main.views.page', name='page_title'),
url(r'^(?P<foo>\w+)/choice/$', 'main.views.lang_pair_choice', name='lang_pair_choice'), 
url(r'^(?P<bar>\w+)/choice2/', include('quiz.urls')),

The problem is that when I pass 2 things into the URL /something/somethingelse it matches the first one and I am not sure how to get it to do something else

1 Answer 1

2

reorder your urls like:

url(r'^(?P<foo>\w+)/choice/$', 'main.views.lang_pair_choice', name='lang_pair_choice'), 
url(r'^(?P<bar>\w+)/choice2/', include('quiz.urls')),
url(r'^(?P<page_lang>\w+)/(?P<page_title>\w+)/$', 'main.views.page', name='page_title'),

Learn more on how Django processes a request here

Sign up to request clarification or add additional context in comments.

5 Comments

neat solution, I will do that this time. In the future if I run across this again, is there any other way to accomplish this?
@deltaskelta Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL. So in the future order your urls based on your requirement. Please check the link provided in my answer for more details
@deltaskelta you just need to make sure you place the more general ones after the more specific ones. If you look at the regexes in this answer, the third one is able to match the first two, so you need to place it after.
Oh yes, I completely understand that part, what I was getting at is asking if ordering is the only way to do it, or if there was some other way if push came to shove. Thanks for the answer
@deltaskelta another way would be to add something before ^(?P<foo>\w+)/choice/$ like ^choices/(?P<foo>\w+)/choice/$ or ^choices/(?P<bar>\w+)/choice2/, this way django will not match the first url and move to check the next one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.