我是一个完整的Django新手,我正在按照教程:
https://docs.djangoproject.com/en/1.11/intro/tutorial01/
我到了舞台:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
在mysite / urls.py中,但是当我这样做时:
python manage.py runserver
我在地址
的浏览器中收到404错误http://localhost:8000/polls/
我正在使用python 2.7.6和Django 1.11.5
我的urls.py是
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
在终端会话中我得到了
Not Found: /polls/
[05/Oct/2017 12:25:22] "GET /polls/ HTTP/1.1" 404 1951
我在浏览器中加载网页
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]
有人可以指出可能出现的问题吗?
答案 0 :(得分:1)
在mysite/settings.py
上添加INSTALLED_APPS
:
INSTALLED_APPS = [
...<other installed apps>..
'polls',
]
对于最新的django版本,建议添加:
INSTALLED_APPS = [
...<other installed apps>..
'polls.apps.PollsConfig',
]
输入mysite/urls.py
(您在此处发布的那个):
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
在polls/urls.py
::
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
答案 1 :(得分:0)
通过遵循教程here,对于遇到此问题的初学者,明确答案,项目根URLconf是与settings.py
在同一文件夹中的那个:
mysite/mysite/urls.py
确保import 'include'
。代码如下:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]
所以在mysite / mysite / settings.py中:
该行应为:
ROOT_URLCONF = 'mysite.urls'
您不需要创建全新的新根URL。