0

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>

1
  • 1
    %2F is encoded url character / , browser will automatically encodes/decodes it. Commented Apr 29, 2020 at 17:49

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

It worked but i wanna know that if i add a new url it should be above login url or at the last.
It depends on the new url. As a rule of thumb, if you hardcode a string like "login/", it should come before any path, that expected a string (like <str:slug>). Because for Django, if <str:slug> at top, "login/" would match that criteria. So it would tread "login" as a slug and use "views.redirect". So now that "login/" is before the slug, it will first check of the string is "login", if not, it will go on to the next path, which is "<str:slug>". And so on...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.