4 项目开发 - 后台
1 Django自带后台系统
- 系统地址
1 urlpatterns = [ 2 url(r'^admin/', admin.site.urls), 3 ]
- 访问:http://127.0.0.1:8000/admin/
- 创建新用户
- 执行命令:createsuperuser
- username:admin
- email:a@163.com
- password:admin123
- 中文修改
1 LANGUAGE_CODE = 'zh-hans' 2 3 TIME_ZONE = 'Asia/Shanghai' 4 5 USE_TZ = False #数据库存储本地时间,非国际时间
- 注册UserProfile
1 from .models import UserProfile 2 3 4 class UserProfileAdmin(admin.ModelAdmin): 5 pass 6 7 admin.site.register(UserProfile, UserProfileAdmin)
2 pip安装xadmin后台管理系统
- 安装:pip install xadmin
- 查看安装包:pip list
1 appdirs (1.4.3) 2 django (1.9.13) 3 django-crispy-forms (1.6.1) 4 django-formtools (2.0) 5 httplib2 (0.9.2) 6 MySQL-python (1.2.5) 7 olefile (0.44) 8 packaging (16.8) 9 Pillow (4.1.1) 10 pip (9.0.1) 11 pyparsing (2.2.0) 12 setuptools (35.0.2) 13 six (1.10.0) 14 wheel (0.29.0) 15 xadmin (0.6.1)
- setting文件中注册app
1 INSTALLED_APPS = [ 2 'django.contrib.admin', 3 'django.contrib.auth', 4 'django.contrib.contenttypes', 5 'django.contrib.sessions', 6 'django.contrib.messages', 7 'django.contrib.staticfiles', 8 'users', 9 'courses', 10 'organization', 11 'operation', 12 'xadmin', 13 'crispy_forms' 14 ]
- urls文件中指向xadmin
1 from django.conf.urls import url 2 from django.contrib import admin 3 import xadmin 4 urlpatterns = [ 5 url(r'^xadmin/', xadmin.site.urls), 6 ]
- admin.py文件中修改注册信息
删除原来注册的内容
- 重启服务进行访问:http://127.0.0.1:8000/xadmin/
- 解决问题:1146, "Table 'mxonline.xadmin_usersettings' doesn't exist"
缺少xadmin默认表
- 查看表更新:makemigrations
- 更新数据库:migrate
3 源码安装xadmin后台管理系统
- 地址:https://github.com
- 搜索:xadmin
- 下载:
- 新建extra_apps目录
- 将xadmin复制到extra_apps下
- 将extra_apps目录mark成source root
- 修改setting文件
1 import os 2 import sys 3 # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 4 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 5 sys.path.insert(0, os.path.join(BASE_DIR, 'apps')) 6 sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
- 更新数据库:
- makemigrations
- migrate
- 卸载xadmin,pip uninstall xadmin
- 重启
- 解决问题:ImportError: No module named future.utils
pip install future
- 访问:http://127.0.0.1:8000/xadmin/
- 新建adminx.py文件用于注册
1 #_*_ encoding:utf-8 _*_ 2 __author__ = 'Aaron' 3 __date__ = '2017/5/30 23:12' 4 5 import xadmin 6 7 from .models import EmailVerifyRecord, Banner 8 9 class EmailVerifyRecordAdmin(object): 10 #后台列表显示列 11 list_display = ['code', 'email', 'send_type', 'send_time'] 12 #后台列表查询条件 13 search_fields = ['code', 'email', 'send_type'] 14 #后天列表通过时间查询 15 list_filter = ['code', 'email', 'send_type', 'send_time'] 16 17 class BannerAdmin(object): 18 #后台列表显示列 19 list_display = ['index', 'title', 'image', 'url', 'add_time'] 20 #后台列表查询条件 21 search_fields = ['index', 'title', 'image', 'url'] 22 #后天列表通过时间查询 23 list_filter = ['index', 'title', 'image', 'url', 'add_time'] 24 25 xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin) 26 xadmin.site.register(Banner, BannerAdmin)
4 xadmin全局配置
- 修改app下的xadmin文件(例如:在users app 下注册)
1 #_*_ encoding:utf-8 _*_ 2 __author__ = 'Aaron' 3 __date__ = '2017/5/30 23:12' 4 5 import xadmin 6 from xadmin import views 7 8 from .models import EmailVerifyRecord, Banner 9 10 class BaseSetting(object): 11 # 主题修改 12 enable_themes = True 13 use_bootswatch = True 14 15 class GlobalSettings(object): 16 #整体配置 17 site_title = '栗+学堂后台系统' 18 site_footer = '栗+学堂' 19 menu_style = 'accordion' #菜单收起 20 21 class EmailVerifyRecordAdmin(object): 22 #后台列表显示列 23 list_display = ['code', 'email', 'send_type', 'send_time'] 24 #后台列表查询条件 25 search_fields = ['code', 'email', 'send_type'] 26 #后天列表通过时间查询 27 list_filter = ['code', 'email', 'send_type', 'send_time'] 28 29 class BannerAdmin(object): 30 #后台列表显示列 31 list_display = ['index', 'title', 'image', 'url', 'add_time'] 32 #后台列表查询条件 33 search_fields = ['index', 'title', 'image', 'url'] 34 #后天列表通过时间查询 35 list_filter = ['index', 'title', 'image', 'url', 'add_time'] 36 37 xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin) 38 xadmin.site.register(Banner, BannerAdmin) 39 xadmin.site.register(views.BaseAdminView, BaseSetting) 40 xadmin.site.register(views.CommAdminView, GlobalSettings)
- 修改菜单
- 修改users app下的apps文件(apps为配置文件)
1 #_*_ encoding:utf-8 _*_ 2 3 from __future__ import unicode_literals 4 5 from django.apps import AppConfig 6 7 8 class UsersConfig(AppConfig): 9 name = 'users' 10 verbose_name = u'用户信息'
2. 修改users app下的__init__文件(指明users app默认配置文件)
1 default_app_config = 'users.apps.UsersConfig'