Elbattore
Elbattore

Reputation: 86

Django urls.py mistaking two close urls

i'm adding forms to my app to modify my lobbys (my custom model).

In urls.py, here is my urlpattern:

urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^lobbys/$', views.LobbyListView.as_view(), name='lobbys'),
url(r'^lobby/(?P<pk>[\w-]+)/$', views.LobbyDetailView.as_view(), name='lobby-detail'),
url(r'^lobby/create/$', views.LobbyCreate.as_view(), name='lobby_create'),
url(r'^lobby/(?P<pk>\d+)/update/$', views.LobbyUpdate.as_view(), name='lobby_update'),
url(r'^lobby/(?P<pk>\d+)/delete/$', views.LobbyDelete.as_view(), name='lobby_delete'),
]

The problem is the following:

The third url is supposed to link to a single lobby template identifed with a UUID field as the primary key. Without the three last lines everything worked fine, but when I added my three urls for the forms, I'm getting the error

Exception Value: ["'create' is not a valid UUID."]

I understand urls.py is taking "create" as a primary key instead of considering it as an urls to an other view.

How can I bypass this problem? Thank you very much.

Upvotes: 0

Views: 109

Answers (1)

karthikr
karthikr

Reputation: 99620

You need to change the ordering of the url patterns

url(r'^lobby/create/$', views.LobbyCreate.as_view(), name='lobby_create'),
url(r'^lobby/(?P<pk>[\w-]+)/$', views.LobbyDetailView.as_view(), name='lobby-detail'),

The issue is, since lobby-detail is looking for a alphanum pattern, it also matches the pattern lobby/create. The order matters in the regex patterns, so lobby_create is never matched. By changing the order, the issue would be resolved

Upvotes: 5

Related Questions