-2

I am using Django v4. This is my urls.py

from django.urls import path
from .views import UserList, UserDetail

urlpatterns = [
    path("users", UserList.as_view()),
]

http://127.0.0.1:8000/users works fine. However, http://127.0.0.1:8000/users/ with the slash at the end fails to work.

To solve this problem, here's the new urls.py.

from django.urls import path
from .views import UserList, UserDetail

urlpatterns = [
    path("users", UserList.as_view()),
    path("users/", UserList.as_view()),
]

It looks kind of repetitive and goes against DRY principle behind Django. Is there a more elegant one-liner to fix this problem? Answer need not be a one-liner as long as it doesn't look repetitive.

EDIT: Best answer I found is here django urls without a trailing slash do not redirect

user3848207
  • 3,737
  • 17
  • 59
  • 104
  • 1
    see https://stackoverflow.com/questions/1596552/django-urls-without-a-trailing-slash-do-not-redirect for a solution. but you should redirect from one to the other. no reason to create ambiguity for such a corner case. – toppk Sep 06 '22 at 03:27
  • Thanks. Updated my question to show the answer you posted. – user3848207 Sep 06 '22 at 03:31

1 Answers1

-1
path("users/", UserList.as_view())

This should cater for both the trailing slash

Akeem
  • 27
  • 6