xtianfab
xtianfab

Reputation: 91

Django Slugs as principal url

I want to do the following, so that the profile of each registered user can be seen as follows: http://127.0.0.1:8000/username/

I was able to achieve this, the problem is when wanting to create another page, for example http://127.0.0.1:8000/explore/

Django consider "explore" as a user and throw me a 404 error.

this is my urls.py

urlpatterns = [
    path("<slug:slug>/", PerfilView.as_view(), name="perfil"),
    path("explore/", UserListView.as_view(), name="explore"),
]

Am I doing it the right way or am I unable to do this with django?

Regads

Upvotes: 2

Views: 36

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476719

You should swap the two path(…): if you visit explore/ it will first match with the PerfilView since the slug can take 'explore' as value.

You thus should rewrite the urlpatterns to:

urlpatterns = [
    #       ↓ first explore
    path('explore/', UserListView.as_view(), name='explore'),
    path('<slug:slug>/', PerfilView.as_view(), name='perfil'),
]

This also means that you can not use explore as a slug here, since then it will match with the UserListView.

Upvotes: 2

Related Questions