The Django admin site is not activated by default – it’s an opt-in thing. To activate the admin site for your installation, do these three things:
-
Uncomment "django.contrib.admin" in the INSTALLED_APPS setting.
-
Run python manage.py syncdb. Since you have added a new application to INSTALLED_APPS, the database tables need to be updated.
-
Edit your mysite/urls.py file and uncomment the lines that reference the admin – there are three lines in total to uncomment. This file is a URLconf; we’ll dig into URLconfs in the next tutorial. For now, all you need to know is that it maps URL roots to applications. In the end, you should have a urls.py file that looks like this:
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', '{{ project_name }}.views.home', name='home'),
# url(r'^{{ project_name }}/', include('{{ project_name }}.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
Django管理站点在默认情况下是没有被激活的,但是可以选择激活。激活管理站点需要做以下三件事情:
1、在settings文件中的INSTALLED_APPS中取消"django.contrib.admin"这一行的注释
2、在命令行输入python manage.py syncdb同步数据库,原因是在INSTALLED_APPS中添加了一个新的app,admin,所以数据库也是需要更新的。
3、编辑你的mysite/urls.py文件,并且参照admin取消注释,总共有三行需要取消。这个urls.py的文件就是URLconf,我们将在下一个教程中深入讲解URLconfs,现在已经了解到如何将URL映射到app了,最后编辑完成的urls.py看起来如下所示。
注意:一定要取消掉admin.autodiscover()前面的注释,它是自动载入在INSTALLED_APPS中的admin.py模块。