python - Django : Page not found (404) Why. ..?

标签 python django

我是一个 Django 初学者。所以我阅读了教程并遵循它们。 但我收到一个错误页面。 '问题是什么...?' (我安装的是python 3.6.5版本)

我的错误:

Using the URLconf defined in mysite.urls, Django tried these URL 
patterns, in this order:
1.polls/
2.admin/
The empty path didn't match any of these.

民意调查/urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

民意调查/views.py

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

最后!! mysite/urls.py

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

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

但是运行python manage.py runserver会出现此错误:

Page not found !!, The empty path didn't match any of these!!!

所以我需要你的帮助,请帮助我......!!

最佳答案

我假设您的mysite是“Django项目名称”,因此它包含rooturls.py。如果我们查看该文件,我们会看到:

#  mysite/urls.py

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

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

因此,这意味着 polls/urls.py 中的所有 urlpatterns 都是此 path('polls/', .. .),因此您可以说 polls/urls.py 中的 URL 的“前缀”为 'polls/'隐式地。

为了到达 index(..) View ,我们必须找到一条从根 URL 到该特定 View 的“路径”。实现此目的的唯一方法是首先采用 'polls/' 路径,然后在 polls/urls.py 中选择 '' 路径。因此路径是 polls/

因此,为了“触发”此 View ,您需要查询如下 URL:

https://localhost:8000/<b>polls/</b>

(当然,如果您指定了其他端口,或者在其他服务器上运行 Django Web 服务器,则需要相应地修改 localhost8000)。

关于python - Django : Page not found (404) Why. ..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52027225/

相关文章:

Django 自定义错误处理

python - 将点云下采样到特定数量的点,同时保持形状

python - 如何根据 Pandas 数据框中的两个或多个子集标准删除重复项

python - Django Python - 如何将外键添加到管理页面中显示的表中

python - 循环遍历 JSON 对象并将结果存储在 pandas dataframe 中

python - 为具有 OnetoOne 关系的 Django 模型创建工厂

python - 为什么这个从另一个字符串中删除第一次出现的字符串的函数无法正常工作?

django - 我从哪里导入 Django 1.10 中的 `DoesNotExist` 异常?

python - 无法在 Django 模板中的 if block 下嵌套 {% static %} 标签

python - Django 中的 Apscheduler Job 执行两次