Django - 开发服务器显示404页面 (http://127.0.0.1:8000/)
我正在熟悉Django这个框架。
我已经成功安装并测试了一个演示网站。现在我想开启管理员模块,看看会发生什么。
我采取的步骤(虽然有些步骤其实没必要,但我只是想确保从头开始是干净的):
- 修改了 mysite/settings.py 文件来启用管理员功能
- 修改了 mysite/url.py 文件来启用管理员功能
- 删除并重新创建了我的后端数据库
- 运行了 ./manage.py syncdb(并正确回答了提示)
- 启动了开发用的网页服务器(./manage.py runserver)
这是我的 mysite/settings.py 文件的相关部分:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# The next lines are my models
'mysite.foo',
'mysite.foobar',
)
这是我的 mysite/urls.py 文件的内容:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
当我访问服务器的URL时,我得到了这个回应:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
1. ^admin/doc/
2. ^admin/
The current URL, , 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.
我哪里做错了?
3 个回答
0
- 使用的是 PyCharm 2.7 版本
- 文件位置:urls.py
- 这一行代码(在 urls.py 文件中)是 url(r'^admin/', include(admin.site.urls)),我把前面的 # 去掉了,然后页面就能正常工作了。
4
请求的网址: http://127.0.0.1:8000/
你正在访问的是根网址。但是,你没有设置与这个网址匹配的配置。你的管理应用在 http://127.0.0.1:8000/admin/
这个地址。试试这个地址吧。
如果你想让管理应用可以在根网址访问,你需要像下面这样修改你的网址映射:
urlpatterns = patterns('',
# Uncomment the next line to enable the admin:
(r'^$', include(admin.site.urls)),
)
请记住,这样做并不推荐。你肯定不想让根网址指向管理应用!
12
很明显,你没有任何网址可以处理请求到 'http://127.0.0.1:8000/'。
要查看管理页面,请访问 'http://127.0.0.1:8000/admin/'。
当你有管理网址时
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# (r'^admin/', include(admin.site.urls)),
如果这些网址被注释掉了,并且没有其他网址,Django 默认会在 'http://127.0.0.1:8000/' 显示欢迎页面。