Django 简明教程
Django - Admin Interface
Django 为管理活动提供了一个即用型的用户界面。我们都知道管理界面对于 Web 项目有多么重要。Django 根据您的项目模型自动生成管理用户界面。
Django provides a ready-to-use user interface for administrative activities. We all know how an admin interface is important for a web project. Django automatically generates admin UI based on your project models.
Starting the Admin Interface
管理界面依赖于 django.countrib 模块。为使其正常工作,您需要确保在 myproject/settings.py 文件的 INSTALLED_APPS 和 MIDDLEWARE_CLASSES 元组中导入了一些模块。
The Admin interface depends on the django.countrib module. To have it working you need to make sure some modules are imported in the INSTALLED_APPS and MIDDLEWARE_CLASSES tuples of the myproject/settings.py file.
对于 INSTALLED_APPS,确保您有 −
For INSTALLED_APPS make sure you have −
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
)
对于 MIDDLEWARE_CLASSES −
For MIDDLEWARE_CLASSES −
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
在启动服务器之前,要访问管理界面,需要初始化数据库 −
Before launching your server, to access your Admin Interface, you need to initiate the database −
$ python manage.py migrate
syncdb 将创建数据库类型所需的必要表格或集合,以便管理界面运行。即使没有超级用户,也会提示您创建一个。
syncdb will create necessary tables or collections depending on your db type, necessary for the admin interface to run. Even if you don’t have a superuser, you will be prompted to create one.
如果您已经拥有超级用户或忘记了超级用户,可以使用以下代码随时创建一个 −
If you already have a superuser or have forgotten it, you can always create one using the following code −
$ python manage.py createsuperuser
现在要启动管理界面,我们需要确保为管理界面配置了一个 URL。打开 myproject/url.py,您应该拥有类似这样的内容 −
Now to start the Admin Interface, we need to make sure we have configured a URL for our admin interface. Open the myproject/url.py and you should have something like −
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myproject.views.home', name = 'home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
现在只需运行服务器即可。
Now just run the server.
$ python manage.py runserver
您的管理界面可以在 [role="bare"] [role="bare"]http://127.0.0.1:8000/admin/ 访问:
And your admin interface is accessible at: [role="bare"]http://127.0.0.1:8000/admin/
一旦使用超级用户帐户连接,您将看到以下屏幕 −
Once connected with your superuser account, you will see the following screen −
该界面将允许您管理 Django 组和用户,以及应用程序中所有注册的模型。该界面使您可以对模型执行至少“CRUD”(创建、读取、更新、删除)操作。
That interface will let you administrate Django groups and users, and all registered models in your app. The interface gives you the ability to do at least the "CRUD" (Create, Read, Update, Delete) operations on your models.