Django管理员界面

Django为管理活动提供随时可以使用的用户界面。我们都知道,管理界面对于一个Web项目是十分重要的。Django根据您的项目模型自动生成管理界面。

启动管理界面

管理界面依赖于 django.contrib 模块。若需它工作,需要确保一些模块是否导入在 myproject/settings.py 文件中的INSTALLED_APPS和MIDDLEWARE_CLASSES元组。

对于 INSTALLED_APPS 确保有 -
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: admin@zyiz.net 
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”(创建,读取,更新,删除)模型操作。



上一篇:Django生命周期

下一篇:Django创建视图

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程