【问题标题】:Page not found error - Writing your first Django app, part 1找不到页面错误 - 编写您的第一个 Django 应用程序,第 1 部分
【发布时间】:2020-02-17 23:27:13
【问题描述】:

我已经在我的 conda 环境中安装了 Django 2.2.5,我正在关注 Writing your first Django app, part 1 的 Django 教程

我完全按照这些步骤操作,但是当我尝试访问 polls/ url 时出现页面未找到错误。

我的根目录结构是这样的

mysite/
    manage.py
    db.sqlite3
    urls.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py
    polls/
        migrations/
        __init__.py
        admin.py
        apps.py
        models.py
        tests.py
        urls.py
        views.py

而我的看法是这样的

D:\TEMP\djangotest\mysite\polls\views.py

from django.shortcuts import render
from django.http import HttpResponse

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

我的两个urls文件是这样的

D:\TEMP\djangotest\mysite\polls\urls.py

from django.urls import path
from . import views

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

D:\TEMP\djangotest\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),
]

我从这样的环境中运行应用程序

(py3.6) D:\TEMP\djangotest\mysite>python manage.py runserver

但是当我转到教程中指示的 url 时,它会给出 404 错误 - 从控制台找不到页面

October 21, 2019 - 16:23:13
Django version 2.2.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /polls
[21/Oct/2019 16:23:19] "GET /polls HTTP/1.1" 404 1954

从浏览器看是这样的

Page not found (404)
Request Method:
GET
Request URL:
http://localhost:8000/polls
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: 
admin/ 
The current path, polls, 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. 

【问题讨论】:

  • 您是否将投票应用添加到您的项目 settings.py 文件中?

标签: django


【解决方案1】:

您的主 urls 文件位于错误的目录中。看起来您已经在基础“mysite”目录中创建了一个单独的 urls.py;相反,您应该在“mysite/mysite”目录中编辑现有的。您创建的那个根本没有被使用。

【讨论】:

    猜你喜欢
    • 2015-08-10
    • 2021-09-04
    • 2018-12-08
    • 2020-08-23
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    相关资源
    最近更新 更多