HARSH ASHRA
HARSH ASHRA

Reputation: 316

Django - URL configuration

Here is something i wanna ask if i try this code i can go to login page but my url look like this http://127.0.0.1:8000/%2Flogin/. What is this %2F?

urlpatterns = [
path("", views.index, name="index"),
path("<str:slug>", views.redirect, name='redirect'),
path('/login/', views.logIn, name='login')]

And i remove slash from login url i get an error message

Page not found (404) Request Method:
GET Request URL:
http://127.0.0.1:8000//login/

after removing slashes here is the code

urlpatterns = [
path("", views.index, name="index"),
path("<str:slug>", views.redirect, name='redirect'),
path('login', views.logIn, name='login')]

So, i wanna know is that why are the slashes affecting the url for login but not <str:slug>

Upvotes: 0

Views: 88

Answers (1)

&#214;zer
&#214;zer

Reputation: 2106

Try this:

urlpatterns = [
    path("login/", views.logIn, name='login'),
    path("<str:slug>/", views.redirect, name='redirect'),
    path("", views.index, name="index")
]

The order of the entries matter and always add a trailing /, unless you have root like views.index

Upvotes: 1

Related Questions