django: Page not found (404)

Question:

Project view

project/urls.py

from django.contrib import admin
from django.urls import path, include
from django.http import HttpResponse

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include('todo.urls')),
]

setting.py

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [os.path.join(BASE_DIR, "templates")],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

todo/ulrs.py

from django.urls import path
from . import views

urlpatterns = [
    path("login", views.home),
]

todo/views.py

from django.http import HttpResponse
from django.shortcuts import render


def home(request):
    return render(request, 'index.html')

enter image description here

I don’t know why it is not showing my page…
I have created django project called taskly. And in that project I have only 1 app called todo. I have referred templates folder as well as you can see above. In that template folder there is only 1 page index.html

Asked By: bizimunda

||

Answers:

Instead of this:

path("login", views.home),

Try this:

path("login/", views.home), #added forward slash here
Answered By: Manoj Tolagekar

yout should add ‘/’ after login and path name,

urlpatterns = [
    path("login/", views.home, name="login"),
]

if it doesn’t work add the HTML link tag in question to let me check

Answered By: Guna

If you want the endpoint as "login" not "login/" then in settings.py add APPEND_SLASH=False by default it is set to be false
check the docs for more info

This is one of the core features of the django where it adds "trailing slash" automatically

the django will 301 redirects automatically.You can see the logs like this

"GET /login HTTP/1.1" 301 0
"GET /login/ HTTP/1.1" 200 6596

Answered By: Ailoni Rohan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.