I have a problem with my urls
en Django
, here my urls.py
:
from .views import (
CouponListView, CouponDetailView, buy_coupon, UserCouponListView, CouponOfResaleCreateView,
CouponOfResaleListView,
)
from django.urls import path, include
coupons_patterns = ([
path('', CouponListView.as_view(), name = 'list'),
path('<slug:slug>/', UserCouponListView.as_view(), name = 'user'),
path('<int:pk>/<slug:slug>/', CouponDetailView.as_view(), name = 'detail'),
path('coupon/<int:pk>/buy-<slug:slug>/', buy_coupon, name = 'buy-coupon'),
path('resale/', include([
path('', CouponOfResaleListView.as_view(), name = 'resale_list'),
path('create/<int:pk>/<slug:slug>/', CouponOfResaleCreateView.as_view(), name = 'create'),
])),
], 'coupons')
The ones urls
that don't work or don't respond are these:
path('resale/', include([
path('', CouponOfResaleListView.as_view(), name = 'resale_list'),
path('create/<int:pk>/<slug:slug>/', CouponOfResaleCreateView.as_view(), name = 'create'),
])),
I don't get an error, they just don't respond, that is, if I want to access one of the urls
ones that I showed previously, for example, resale_list
instead of showing me what it should show me, it shows me the result of accessing the url
user
.
In the url
browser is the url
correctly but it shows me the HTML of another url
, which in this case is the url
user
.
The way to fix it is to move that portion on urls
top, as follows:
coupons_patterns = ([
path('resale/', include([
path('', CouponOfResaleListView.as_view(), name = 'resale_list'),
path('create/<int:pk>/<slug:slug>/', CouponOfResaleCreateView.as_view(), name = 'create'),
])),
path('', CouponListView.as_view(), name = 'list'),
path('<int:pk>/<slug:slug>/', CouponDetailView.as_view(), name = 'detail'),
path('coupon/<int:pk>/buy-<slug:slug>/', buy_coupon, name = 'buy-coupon'),
path('<slug:slug>/', UserCouponListView.as_view(), name = 'user'),
], 'coupons')
Another solution is to move the url
user
below said portion of urls
, as follows:
coupons_patterns = ([
path('', CouponListView.as_view(), name = 'list'),
path('<int:pk>/<slug:slug>/', CouponDetailView.as_view(), name = 'detail'),
path('coupon/<int:pk>/buy-<slug:slug>/', buy_coupon, name = 'buy-coupon'),
path('resale/', include([
path('', CouponOfResaleListView.as_view(), name = 'resale_list'),
path('create/<int:pk>/<slug:slug>/', CouponOfResaleCreateView.as_view(), name = 'create'),
])),
path('<slug:slug>/', UserCouponListView.as_view(), name = 'user'),
], 'coupons')
But why does this happen? Because depending on the order of the urls
causes this problem to happen?
Any solution? Since I don't like to have my files urls
in that order for them to work.
The reason why this is happening to you is very simple, each of the urls is a
regex
. Django checks one by one if the url entered matches the urls you have indicated inurls.py
. The moment he finds a match, he uses it. For this reason it works for you when reordering the urls. I would recommend using more specific urls./recurso1/id/id
/recurso2/id/id