==========================================================================
1: 创建虚拟环境(可选择用或不用虚拟环境)
mkvirtualenv
安装
pip install django==1.9
pip install python-mysql
pip install Pillow
================================================================ =========
后台admin用的xadmin 需安装以下依赖
Django=>1.9
django-crispy-forms==1.6.1
django-formtools==1.0
django-pure-pagination==0.3.0
django-simple-captcha==0.4.6
DjangoUeditor==1.8.143
httplib2==0.9.2
Pillow==3.4.2==========================================================================
创建 users APP startapp users 并设计models
# 在settings.py配置 重载Django自带的user表 AUTH_USER_MODEL = 'users.UserProfile'
# _*_ coding: utf-8 _*_ from __future__ import unicode_literals from django.db import models from django.contrib.auth.models import AbstractUser from datetime import datetime class UserProfile(AbstractUser): """ 用户表设计 如果Django自带的用户表无法满足的时候继承原表增加自己的字段 """ nick_name = models.CharField(max_length=50, verbose_name=u'昵称', default='') birthday = models.DateField(verbose_name=u'生日', blank=True, null=True) gender = models.CharField(choices=(('male', u'男'), ('female', u'女')), default='female', max_length=6) address = models.CharField(max_length=100, default='') mobile = models.CharField(max_length=11, null=True, blank=True) image = models.ImageField(upload_to='image/%Y/%m', default=u'image/default.png', max_length=100) class Meta: verbose_name = u'用户信息' verbose_name_plural = verbose_name def __unicode__(self): return self.username class EmailVerifyRecord(models.Model): """ 邮箱验证码表设计 由于验证码只和Users有关系所以把表设计在UsersModels里 """ code = models.CharField(max_length=20, verbose_name=u'验证码') email = models.EmailField(max_length=50, verbose_name=u'邮箱') send_type = models.CharField( choices=(('register', u'注册'), ('forget', u'找回密码')), max_length=10) send_time = models.DateTimeField(default=datetime.now) class Meta: verbose_name = u'邮箱验证码' verbose_name_plural = verbose_name class Banner(models.Model): """ 轮播图表 设计 """ title = models.CharField(max_length=100, verbose_name=u'标题') image = models.ImageField(upload_to='banner/%Y/%m', verbose_name=u'轮播图', max_length=100) url = models.URLField(max_length=200, verbose_name=u'访问地址') index = models.IntegerField(default=100, verbose_name=u'顺序') add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') class Meta: verbose_name = u'轮播图'
verbose_name_plural = verbose_name
创建课程app startapp courses
# _*_ coding: utf-8 _*_ from __future__ import unicode_literals from datetime import datetime from django.db import models class Course(models.Model): """ CoursesAPP models设计 课程表设计 """ name = models.CharField(max_length=50, verbose_name=u'课程名') desc = models.CharField(max_length=300, verbose_name=u'课程描述') detail = models.TextField(verbose_name=u'课程详情') degree = models.CharField( choices=(('cj', u'初级'), ('zj', u'中级'), ('gj', u'高级')), max_length=2) learn_times = models.IntegerField(default=0, verbose_name=u'学习时长(分钟数)') students = models.IntegerField(default=0, verbose_name=u'学习人数') fav_nums = models.IntegerField(default=0, verbose_name=u'收藏人数') image = models.ImageField(upload_to='course/%Y/%m', verbose_name='封面图', max_length=100) click_nums = models.IntegerField(default=0, verbose_name=u'点击数') add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') class Meta: verbose_name = u'课程' verbose_name_plural = verbose_name class Lesson(models.Model): """ 课程章节表设计 课程表与章节表是一对多的关系 章节表外键引用课程表 """ course = models.ForeignKey(Course, verbose_name=u'课程') name = models.CharField(max_length=100, verbose_name=u'章节') add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') class Meta: verbose_name = u'章节' verbose_name_plural = verbose_name class Video(models.Model): """ 视频表设计 视频表与章节表是一对多关系 视频表外键引用章节表 """ lesson = models.ForeignKey(Lesson, verbose_name=u'章节') name = models.CharField(max_length=100, verbose_name=u'视频') add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') class Meta: verbose_name = u'视频' verbose_name_plural = verbose_name class CourseResource(models.Model): """ 课程资源下载表设计 引用课程外键 """ course = models.ForeignKey(Course, verbose_name=u'课程') name = models.CharField(max_length=100, verbose_name=u'名称') download = models.FileField(upload_to='course/resource/%Y/%m', verbose_name=u'课程资源', max_length=100) add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') class Meta: verbose_name = u'课程资源' verbose_name_plural = verbose_name
创建机构APP startapp organization 并设计models
# _*_ coding: utf-8 _*_ from __future__ import unicode_literals from datetime import datetime from django.db import models class CityDict(models.Model): """ 城市表设计 """ name = models.CharField(max_length=20, verbose_name=u'城市名字') desc = models.CharField(max_length=200, verbose_name=u'城市描述') add_time = models.DateTimeField(default=datetime.now) class Meta: verbose_name = u'城市' verbose_name_plural = verbose_name class CourseOrg(models.Model): """ 课程机构表设计 """ name = models.CharField(max_length=50, verbose_name=u'机构名称') desc = models.TextField(verbose_name=u'机构描述') click_nums = models.IntegerField(default=0, verbose_name=u'点击数') fav_nums = models.IntegerField(default=0, verbose_name=u'收藏数') image = models.ImageField(upload_to='org/%Y/%m', verbose_name='封面图', max_length=100) address = models.CharField(max_length=150, verbose_name=u'机构地址') city = models.ForeignKey(CityDict, verbose_name=u'所在城市') add_time = models.DateTimeField(default=datetime.now) class Meta: verbose_name = u'课程机构' verbose_name_plural = verbose_name class Teacher(models.Model): """ 教师表设计 引用外键课程机构 """ org = models.ForeignKey(CourseOrg, verbose_name=u'所属机构') name = models.CharField(max_length=20, verbose_name=u'教师名字') work_years = models.IntegerField(default=0, verbose_name=u'工作年限') work_company = models.CharField(max_length=50, verbose_name=u'就职公司') work_position = models.CharField(max_length=50, verbose_name=u'公司职位') points = models.CharField(max_length=50, verbose_name=u'教学特点') click_nums = models.IntegerField(default=0, verbose_name=u'点击数') fav_nums = models.IntegerField(default=0, verbose_name=u'收藏数') add_time = models.DateTimeField(default=datetime.now) class Meta: verbose_name = u'教师' verbose_name_plural = verbose_name
创建operationAPP startapp operation
# _*_ coding: utf-8 _*_ from __future__ import unicode_literals from datetime import datetime from django.db import models from users.models import UserProfile from courses.models import Course class UserAsk(models.Model): """ 用户咨询表 """ name = models.CharField(max_length=20, verbose_name=u'姓名') mobile = models.CharField(max_length=11, verbose_name=u'手机') course_name = models.CharField(max_length=50, verbose_name=u'课程名称') add_time = models.DateTimeField(datetime.now, verbose_name=u'添加时间') class Meta: verbose_name = u'用户咨询' verbose_name_plural = verbose_name class CourseComments(models.Model): """ 课程评论表 """ user = models.ForeignKey(UserProfile, verbose_name=u'用户') course = models.ForeignKey(Course, verbose_name=u'课程') comments = models.CharField(max_length=200, verbose_name=u'评论') add_time = models.DateTimeField(datetime.now, verbose_name=u'添加时间') class Meta: verbose_name = u'课程评论' verbose_name_plural = verbose_name class UserFavorite(models.Model): """ 用户收藏表 """ user = models.ForeignKey(UserProfile, verbose_name=u'用户') fav_id = models.IntegerField(default=0, verbose_name=u'数据id') fav_type = models.IntegerField( choices=((1, u'课程'), (2, u'课程机构'), (3, u'授课老师')), default=1, verbose_name=u'收藏类型') add_time = models.DateTimeField(datetime.now, verbose_name=u'添加时间') class Meta: verbose_name = u'用户收藏' verbose_name_plural = verbose_name class UserMessage(models.Model): """ 用户消息表 """ user = models.IntegerField(default=0, verbose_name=u'接收用户') message = models.CharField(max_length=500, verbose_name=u'消息内容') has_read = models.BooleanField(default=False, verbose_name=u'是否已读') add_time = models.DateTimeField(datetime.now, verbose_name=u'添加时间') class Meta: verbose_name = u'用户消息' verbose_name_plural = verbose_name class UserCourse(models.Model): """ 用户课程表 """ user = models.ForeignKey(UserProfile, verbose_name=u'用户') course = models.ForeignKey(Course, verbose_name=u'课程') add_time = models.DateTimeField(datetime.now, verbose_name=u'添加时间') class Meta: verbose_name = u'用户课程' verbose_name_plural = verbose_name
=======================================================================================
在项目同级目录创建apps文件包
将新建的4个app移动到apps方便管理
并且在settings.py中配置
import sys
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))否则运行会出现找不到Models错误
迁移文件 python.manage.py makemigrations
生成数据库 python.manage.py migrate
这时MySql数据库表建立完成
创建admin超级管理 createsupperuser
写入用户名 邮箱 密码
在浏览器输入127.0.0.1:8000/admin登录 后台界面
在Settings.py中配置后台管理为中文显示
在Users app下admin.py 中将用户注册到后台
from django.contrib import admin from .models import UserProfile class UserProfileAdmin(admin.ModelAdmin): pass admin.site.register(UserProfile, UserProfileAdmin)
增加用户操作出现外键约束错误
解决办法在settings.py mysql配置中添加:
'OPTIONS':{
"init_command":"SET foreign_key_checks = 0;",
}
取消外键检查
安装Xadmin pip install xadmin 也可以选择源码安装在GitHub中下载
在项目urls里配置xadmin路径
import xadmin from django.conf.urls import url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^xadmin/', xadmin.site.urls), ]在浏览器中输入 http://127.0.0.1:8000/xadmin/
Xadmin 后台管理页面
接下来将设计的Models注册到Xadmin后台管理页面中
在UsersAPP下新建adminx.py文件 (Xadmin会自动搜索App下的adminx.py文件)
# !/usr/bin/env python # -*- coding:utf-8 -*- __author__ = '_X.xx_' __date__ = '2018/6/7 20:23' import xadmin from .models import EmailVerifyRecord class EmailVerifyRecordAdmin(object): """ 注册邮箱验证表到xadmin后台管理页面 """
# 后台显示列 list_display = ['code', 'email', 'send_type', 'send_time']
# 通过字段进行搜索 search_fields = ['code', 'email', 'send_type']xadmin .site . register (EmailVerifyRecord , EmailVerifyRecordAdmin )
class BannerAdmin(object): """ 注册轮播图到xadmin后台管理页面 """ list_display = ['title', 'image', 'url', 'index', 'add_time'] # 通过字段进行搜索 search_fields = ['title', 'image', 'url', 'index'] # 过滤器筛选功能 list_filter = ['title', 'image', 'url', 'index', 'add_time'] xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin) xadmin.site.register(Banner, BannerAdmin)
将课程APP中的models注册到xadmin后台管理页面
在course app中新建adminx.py
# !/usr/bin/env python # -*- coding:utf-8 -*- __author__ = '_X.xx_' __date__ = '2018/6/7 23:11' from .models import * import xadmin class CourseAdmin(object): """ 把课程表注册到xadmin后台管理页面 """ list_display = ['name', 'desc', 'detail', 'degree', 'learn_times', 'students', 'fav_nums', 'click_nums', 'add_time'] search_fields = ['name', 'desc', 'detail', 'degree', 'learn_times', 'students', 'fav_nums', 'click_nums'] list_filter = ['name', 'desc', 'detail', 'degree', 'learn_times', 'students', 'fav_nums', 'click_nums', 'add_time'] xadmin.site.register(Course, CourseAdmin)
是不是菜单栏看起来很乱?接下来优化一下 在Users app 下adminx.py中写入
from xadmin import views class BaseSetting(object): enable_themes = True use_bootswatch = True class GlobalSetting(object): site_title = '_X.xx_后台管理系统' site_footer = '_X.xx_在线网' menu_style = 'accordion' xadmin.site.register(views.BaseAdminView, BaseSetting) xadmin.site.register(views.CommAdminView, GlobalSetting)
修改后的后台管理页面 增加菜单折叠功能 主题功能 页头名 底部名的修改 看起来很炫酷
接下来自定义显示APP名称
在每个APP下面都有个apps.py文件在里面添加
# _*_ coding: utf-8 _*_ 还有这个 from django.apps import AppConfig class UsersConfig(AppConfig): name = 'users' verbose_name = u'用户信息' # 这行
并且在每个APP下的__init__.py下添加对应的config
default_app_config = 'users.apps.UsersConfig'
更改完成后的界面 就可以自定义显示了