我是 Django 的新手,前一天刚开始通过在我的本地安装 Django 1.10 来查看它。
我已按照此链接 https://docs.djangoproject.com/en/dev/intro/tutorial01/ 的所有说明进行操作。但是我不断收到此错误:
Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, polls/, didn't match any of these.
我已经创建了民意测验应用程序以开始使用。
首先,我使用了 view.py,代码如下:
投票/views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World!")
这是我的 urls.py 代码:
投票/urls.py
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
这是根目录中的 urls.py:
我的网站/urls.py
from django.conf.urls import patterns,include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
我花了很多时间来找出解决方案,发现这个问题已经被问了很多次,所以也尝试了这些解决方案,但没有一个对我有用。
我找不到我在这里遗漏的东西,所以请提请我注意差距。
原文由 Dhaval 发布,翻译遵循 CC BY-SA 4.0 许可协议
我认为您在尝试更改根 URL 配置时编辑了错误的文件。
Make sure you are editing the root url config in
mysite/mysite/urls.py
(the directory containingsettings.py
) notmysite/urls.py
(the directory containingmanage.py
).作为一般建议,安装最新版本,目前为 1.9。不要使用正在开发中的 1.10。确保您遵循的 是 1.9 的教程,因为教程会针对不同的版本进行更改。例如,您的
mysite/urls.py
与 1.9 的教程不匹配,因为urlpatterns
应该是: