Django 404 Ошибка, представления не найдены. Почему? Представление не отображается или не найдено

Ранее я программировал на 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),
]
  • Вот мое дерево: polls/ --> init.py -> admin.py -> apps.py -> migrations/ --> init.py -> models.py -> tests.py -> urls.py -> views.py ->


Конечно, я запустил django-admin startproject mysite до всех тихов, и вывод для версии таков:

└──╼ $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.

... но когда я нажимаю на ссылку, я получаю эту ошибку:


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/ или http://127.0.0.1:8000/admin/

Ваш polls/url.py основан на пути 'polls/' (определен на mysite/urls.py)

На самом деле у вас определено только два пути :

  • опросы/
  • admin

  • Если вы добавите polls/url.py

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

    У вас также будет определен путь polls/index

    Вернуться на верх