- Django基础
- Django开发环境安装配置
- Django创建工程
- Django生命周期
- Django管理员界面
- Django创建视图
- Django URL映射
- Django模板系统
- Django模型
- Django页面重定向
- Django发送E-mail
- Django通用视图
- Django表单处理
- Django上传文件
- Django Apache配置
- Django Cookies处理
- Django Session会话
- Django缓存
- Django RSS
- Django Ajax应用
- Django快速入门
- Django快速入门-数据库模型
- Django快速入门-视图
- Django快速入门-表单
Django管理员界面
启动管理界面
管理界面依赖于 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: 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创建视图
扫描二维码
程序员编程王