管理界面依賴于 django.contrib 模塊。若需它工作,需要確保一些模塊是否導(dǎo)入在 myproject/settings.py 文件中的INSTALLED_APPS和MIDDLEWARE_CLASSES元組。
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp', )
對(duì)于 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', )
c:\myproject> python manage.py syncdb
syncdb將創(chuàng)建必要的表,或根據(jù)您的數(shù)據(jù)庫(kù)類型的集合,以及必要的管理界面來(lái)運(yùn)行。 即使你不是一個(gè)超級(jí)用戶,系統(tǒng)會(huì)提示創(chuàng)建一個(gè)。
如果你已經(jīng)有一個(gè)超級(jí)用戶或忘記了,可以用下面的代碼來(lái)直接創(chuàng)建一個(gè) ?
c:\myproject> python manage.py createsuperuser
現(xiàn)在就開始啟動(dòng)管理界面,我們需要確保已經(jīng)為管理界面配置URL。打開 myproject/url.py,應(yīng)該有這樣的東西 ?
"""myproject URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
# 創(chuàng)建必要的數(shù)據(jù)庫(kù)表,并初始化相關(guān)數(shù)據(jù) C:\myproject>python manage.py migrate Operations to perform: Apply all migrations: admin, contenttypes, auth, sessions Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying sessions.0001_initial... OK C:\myproject>python manage.py createsuperuser Username (leave blank to use 'administrator'): admin Email address: admin@yiibai.com Password: Password (again): This password is too short. It must contain at least 8 characters. This password is too common. This password is entirely numeric. Password: Password (again): Superuser created successfully. C:\myproject>
c:\myproject> python manage.py runserver
管理界面如下URL,應(yīng)該是可以訪問(wèn):http://127.0.0.1:8000/admin/

使用超級(jí)用戶帳號(hào)登陸,會(huì)看到如下界面 ?

這個(gè)界面可以讓我們管理 Django 中的組和用戶,以及所有在應(yīng)用程序中注冊(cè)的模型。這個(gè)界面使您能夠至少做到“CRUD”(創(chuàng)建,讀取,更新,刪除)模型操作。