简体   繁体   中英

django: Page not found (404)

项目视图

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')

在此处输入图像描述

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

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 Django where it adds a "trailing slash" automatically.

Django will redirect 301 automatically. You can see the logs like this:

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

Instead of this:

path("login", views.home),

Try this:

path("login/", views.home), #added forward slash here

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM