Hyungmin Cho
Hyungmin Cho

Reputation: 13

Django URL dispatcher - it calls the same view to the different URL(regular expression)

Hello thank you very much for reading my question post.

I have different url path patterns in urlpatterns, but Django URL dispatcher(re-path) calls the same view( views.selected_verb) for the different URL expressed by Regular expression.

These urls call the same view(views.selected_verb) http://127.0.0.1:8000/arabic/verbs/%D9%83%D8%A7%D9%86/ http://127.0.0.1:8000/arabic/verbs/%D9%83%D8%A7%D9%86/quiz/

Would love to know how to fix it(calls different views)

here is urlpatterns

urlpatterns = [
    path('', views.index, name='index'),
    path('verbs', views.verbs, name='verbs'),
    re_path(r'^verbs/(?P<verb>.+)/$', views.selected_verb, name='selected_verb'),
    re_path(r'^verbs/(?P<verb>.+)/quiz/$', views.quiz, name='quiz'),
]

Thank you very much again!

Upvotes: 1

Views: 291

Answers (1)

briancaffey
briancaffey

Reputation: 2559

I think the issue is that .+ will match with anything, which includes %D9%83%D8%A7%D9%86/quiz/. Maybe you could try telling it something more explicit, like [A-Z0-9%]+. When the q character comes along in quiz, it will fail matching and then go to the next url pattern which should be the one you want.

So I think it should all look like this:

urlpatterns = [
    path('', views.index, name='index'),
    path('verbs', views.verbs, name='verbs'),
    re_path(r'^verbs/(?P<verb>[A-Z0-9%]+)/quiz/$', views.quiz, name='quiz'),
    re_path(r'^verbs/(?P<verb>[A-Z0-9%]+)/$', views.selected_verb, name='selected_verb'),
]

Upvotes: 0

Related Questions