找不到 Django 页面,尽管一切看起来都不错

问题描述 投票:0回答:1

我是 Django 新手,我正在执行 Django 文档中指示的所有操作,但我不明白为什么它一直显示“找不到页面”

我尝试将路径或 Debug=True 更改为 Debug=False 但没有发生任何有趣的事情。它曾经有效,但现在一直显示页面未找到。

from django.urls import path

from . import views

urlpatterns = [
    path("mainpage/", views.index, name="index"),
]

这是 urls.py 主页

from django.shortcuts import render


from django.http import HttpResponse


def index(request):
    return HttpResponse("My first page")

这是views.py主页

from django.contrib import admin
from django.urls import path
from django.contrib import admin
from django.urls import include, path
urlpatterns = [

    path("mainpage/", include("mainpage.urls")),
    path('admin/', admin.site.urls),
]

```and this one is urls.py mysite 

this is the result:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

mainpage/ mainpage/ [name='mainpage']
admin/
The current path, mainpage/, didn’t match any of these.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
http://127.0.0.1:8000/mainpage/
python django url frameworks 404-page
1个回答
0
投票

我认为您设置正确网址的方式将是主页/主页。

通过在项目的 url 中包含此代码 path("mainpage/", include("mainpage.urls")),在 mainpage.urls 中找到的所有路径都将以 mainpage/ 开头。通过在应用程序 URL 中再次包含 mainpage/,您最终会得到 mainpage/mainpage。

我希望这就是问题所在并有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.