is this a Django BUG in urls.py ?

Hi so i have successfully implemented a profile page and also a post detail page (Thanks to this community for guiding me through). But here is the problem. do the order by which the urls are listed matters ?

Now here is the Bug:

from django.urls import path
from .views import Home,Data,Test,SearchResults,PostListView,PostDetailView,Profile

urlpatterns = [
    path("", Home, name = "Home"),
    path("data/", Data, name = "Data"),
    path("test/", Test, name = "Test"),
    path('search/', SearchResults, name='search_results'),
    path("posts/", PostListView.as_view(), name = "Post"),
    path("<str:author>/", Profile, name = "Profile"),
    path("<slug:slug>/", PostDetailView.as_view(), name = "Post-Detail"),
]

In the Above Code, the last URL does not work, as when i type the slug, it just shows empty with no errors what so ever. But the Author Link Works.

Now if i rearrange them like this :

from django.urls import path
from .views import Home,Data,Test,SearchResults,PostListView,PostDetailView,Profile

urlpatterns = [
    path("", Home, name = "Home"),
    path("data/", Data, name = "Data"),
    path("test/", Test, name = "Test"),
    path('search/', SearchResults, name='search_results'),
    path("posts/", PostListView.as_view(), name = "Post"),
    path("<slug:slug>/", PostDetailView.as_view(), name = "Post-Detail"),  
   path("<str:author>/", Profile, name = "Profile"),   
]

Now here the Slug Link works, but The Author link Does not Work… Please help :frowning:

Yes, the order of url definitions matter. See How Django processes a request. The first matching pattern will apply, and since every slug is also a string, there’s no intrinsic way to tell them apart.
Likewise, if you had a slug or author named “data”, “test”, “search” or “posts”, those would never get viewed.

1 Like

Hi i solved it by :slight_smile:

path("profile/<str:author>/", Profile, name = "Profile"),

Thanks you so much :slight_smile: