为什么 Django 给我一个 404 错误 [英] why is Django giving me a 404 error
问题描述
我目前正在尝试完成 Django 2.0 教程 Poll 应用程序.我使用的是 Django 2.0.7 版和 Python 3.7.0.我已经为这个项目建立了一个虚拟环境.我对 Django 非常陌生,如果我的术语不正确,请见谅.
I am currently trying to complete the Django 2.0 tutorial Poll application. I am using Django version 2.0.7 and Python 3.7.0. I have set up a virtual environment for this project. I am very new to Django so apologies if my terminology is incorrect.
基本上,我无法让第 1 部分工作.
Basically, I cant get part 1 to work.
我已经多次尝试运行它,但似乎无法弄清楚为什么它不起作用.
I have tried numerous times to run it and I cant seem to figure out why it wont work.
在我创建 Polls 应用程序之前,站点运行正常并且我得到了火箭飞船,但是在我创建应用程序之后,更新 polls/views.py,创建 polls/urls.py 文件并更新 mysite/urls.py,我收到以下错误:
Before I create the Polls app the site runs correctly and I get the rocket ship, however after I create the app, update polls/views.py, create the polls/urls.py file and update the mysite/urls.py, I get the following error:
找不到页面 (404)请求方法 GET请求网址:http://127.0.0.1:8000/
Page not found (404) Request method GET Request URL: http://127.0.0.1:8000/
使用 mysite.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
- 民意调查/
- 管理员/
空路径与这些都不匹配.
The empty path didn't match any of these.
我认为我的代码不正确,所以我直接从网站上复制并粘贴了代码并尝试运行它.还是不行.
I thought my code was incorrect, so I copied and pasted the code from the website directly and tried to run it. It still wont work.
下面是我正在尝试的代码和目录布局.任何帮助将不胜感激.我确定我只是缺少一些简单的东西.
Below is the code I am trying and the directory layout. Any help would be greatly appreciated. I'm sure I am just missing something simple.
谢谢.
mysite/urls.py
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),
]
民意调查/urls.py
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'), ]
民意调查/views.py
polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
C:\Users\ad\Documents\projects\mysite\msenv\mysite\mysite 目录
Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\mysite
打印.txt
settings.py
settings.py
urls.py
wsgi.py
init.py
pycache
C:\Users\ad\Documents\projects\mysite\msenv\mysite\polls 目录
Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\polls
admin.py
apps.py
迁移
models.py
tests.py
urls.py
views.py
init.py
pycache
推荐答案
您是否在 settings.py 中添加了您的应用名称,请确保您已在位于项目文件夹下的 settings.py 中添加了名称
have you added your app name inside settings.py make sure you have added the name in settings.py which resides under project folder
就您而言,它将在这里:
in your case it will be here:
C:\Users\ad\Documents\projects\mysite\msenv\mysite\mysite 目录
Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\mysite
像这样:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name',] # <--- polls in your case
确保您使用已定义的路由访问正确的页面
make sure you are using your routes you have defined to access the correct page
例如在您的情况下:
您网站的路线是:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
并且您的应用程序民意调查"路线是:
and your application 'polls' routes are:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
根据你的views.py文件
according to your views.py file
这是:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
您必须访问 http://localhost:{port number}/polls/或 http://127.0.0.1:{端口号}/polls/
you must visit http://localhost:{port number}/polls/ or http://127.0.0.1:{port number}/polls/
默认端口号为 8000
default port number is 8000
使用 python manage.py runserver 运行服务器后
after running server using python manage.py runserver
让您的页面作为浏览器的输出
to get your page as an output on browser
这篇关于为什么 Django 给我一个 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!