Home > Software engineering >  Django Create View URL conflicts with Single View URL
Django Create View URL conflicts with Single View URL

Time:12-05

My url.py file is as below.

urlpatterns = [
path('', views.loans, name='loans'),
path('loans/<str:pk>/', views.loan, name='loan'),
path('loans/create/', views.create_loan, name='create-loan'),
]

Whenever I try to access the loans/create route, Django throws the below exception.

ValidationError at /loans/create/
['“create” is not a valid UUID.']

It looks like Django is passing 'create' into 'loans/<str:pk>/'

How can I resolve this?

Thanks in advance.

CodePudding user response:

URL patterns are iterated over in order and the first match is returned, the pk parameter in your loan url will always match the string "create". You need to swap them around so that the create url is tested first

urlpatterns = [
    path('', views.loans, name='loans'),
    path('loans/create/', views.create_loan, name='create-loan'),
    path('loans/<str:pk>/', views.loan, name='loan'),
]
  • Related