管理介面依賴於 django.contrib 模組。若需它工作,需要確保一些模組是否匯入在 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', )
對於 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將建立必要的表,或根據您的資料庫型別的集合,以及必要的管理介面來執行。 即使你不是一個超級使用者,系統會提示建立一個。
如果你已經有一個超級使用者或忘記了,可以用下面的程式碼來直接建立一個 ?
c:\myproject> python manage.py createsuperuser
現在就開始啟動管理介面,我們需要確保已經為管理介面組態URL。開啟 myproject/url.py,應該有這樣的東西 ?
"""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), ]
# 建立必要的資料庫表,並初始化相關資料 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: [email protected] 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,應該是可以存取:http://127.0.0.1:8000/admin/
使用超級使用者帳號登陸,會看到如下介面 ?
這個介面可以讓我們管理 Django 中的組和使用者,以及所有在應用程式中註冊的模型。這個介面使您能夠至少做到「CRUD」(建立,讀取,更新,刪除)模型操作。