Django 404 错误,未找到视图。为什么?视图未显示或未找到

Django 404 Error, views not found. Why? View is not show up or is not found

我事先在 Ruby 中编程,现在正在转向 Django。我正在尝试关注此处的文章。 https://docs.djangoproject.com/en/4.0/intro/tutorial01/

#polls/url.py
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]
# mysite /urls.py
"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path

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

当然,我 运行 django-admin startproject mysite 在所有 tihs 之前,版本的输出是:

└──╼ $python -m django --version
4.0.4

我尝试启动服务器:

─╼ $python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
May 24, 2022 - 13:47:13
Django version 4.0.4, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

...但是当我单击 link 时出现此错误:


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

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

    polls/
    admin/

The empty path 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.

其中常见的 404 错误。为什么会这样?我 运行 一切都按照指示进行。也许sqlite服务器有问题?我很迷茫,任何帮助将不胜感激,

您没有任何根路由。所以添加

urlpatterns = [
    path('', **something**),
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

应该帮忙或尝试打开http://127.0.0.1:8000/polls/ or http://127.0.0.1:8000/admin/

您的 polls/url.py 基于路径 'polls/'(在 mysite/urls.py 上定义)

实际上你只定义了两条路径:

  • 民意调查/
  • 管理员

  • 如果你加入 polls/url.py

    path('index', views.index, name='index')

    您还将定义路径 polls/index