Django+xadmin打造在线教育平台(从0-1全过程手把手教程附加各种采坑笔记第5章)

Django+xadmin打造在线教育平台(从0-1全过程手把手教程附加各种采坑笔记第5章)_第1张图片
附赠上个博客遗留的问题:这里如果要显示中文信息,在Django+xadmin打造在线教育平台(从0-1全过程手把手教程附加各种采坑笔记第5章)_第2张图片
Django+xadmin打造在线教育平台(从0-1全过程手把手教程附加各种采坑笔记第5章)_第3张图片
这两个地方要配置一下。
同理可得,在organization文件夹中,配置models和adminx文件跟users文件夹类似。首先`from datetime import datetime

from django.db import models

Create your models here.

class CityDict(models.Model):
name = models.CharField(max_length=20,verbose_name=u’城市’)
desc = models.TextField(verbose_name=u’描述’)
add_time = models.DateTimeField(default=datetime.now)

class Meta:
    verbose_name = u'城市'
    verbose_name_plural = verbose_name

def __str__(self):
    return self.name

class CourseOrg(models.Model):
name = models.CharField(max_length=50,verbose_name=u’机构名称’)
desc = models.TextField(verbose_name=u’机构描述’)
category = models.CharField(default=‘pxjg’,verbose_name=u’机构类别’,max_length=20,choices=((‘pxjg’,‘培训机构’),(‘gr’,‘个人’),(‘gx’,‘高校’)))
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=u’logo’)
address = models.CharField(max_length=150,verbose_name=u’机构地址’)
city = models.ForeignKey(CityDict,verbose_name=u’所在城市’,on_delete=models.CASCADE)
students = models.IntegerField(default=0,verbose_name=u’学习人数’)
course_nums = models.IntegerField(default=0,verbose_name=u’课程数’)
add_time = models.DateTimeField(default=datetime.now)
tag = models.CharField(default=‘全国知名’,max_length=10,verbose_name=u’机构标签’)

class Meta:
    verbose_name = u'课程机构'
    verbose_name_plural = verbose_name

def __str__(self):
    return self.name

class Teacher(models.Model):
org = models.ForeignKey(CourseOrg,verbose_name=u’所属机构’,on_delete=models.CASCADE)
name = models.CharField(max_length=50,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’收藏数’)
age = models.IntegerField(default=18,verbose_name=u’年龄’)
image = models.ImageField(‘头像’, upload_to=‘teacher/%Y/%m’, max_length=100, default=’’)
add_time = models.DateTimeField(default=datetime.now)

class Meta:
    verbose_name = u'教师'
    verbose_name_plural = verbose_name

def __str__(self):
    return self.name`
  1. 这里models建立数据库字段,前面每段代码都解释差不多,这里就不赘述了。
  2. `# * coding: utf-8 *
    import xadmin
    from apps.organization.models import CityDict, CourseOrg, Teacher

author = ‘hzy’
date = ‘2020/2/3 20:48’

class CityDictAdmin(object):
list_display = [‘name’,‘desc’,‘add_time’]
search_fields =[‘name’,‘desc’]
list_filter = [‘name’,‘desc’,‘add_time’]

class CourseOrgAdmin(object):
list_display = [‘name’,‘desc’,‘click_nums’,‘fav_nums’,‘image’,‘address’,‘city’,‘add_time’]
search_fields =[‘name’,‘desc’,‘click_nums’,‘fav_nums’,‘image’,‘address’,‘city’]
list_filter = [‘name’,‘desc’,‘click_nums’,‘fav_nums’,‘image’,‘address’,‘city’,‘add_time’]

class TeacherAdmin(object):
list_display =[‘org’,‘name’,‘work_years’,‘work_company’,‘work_position’,‘points’,‘click_nums’,‘fav_nums’,‘add_time’]
search_fields =[‘org’,‘name’,‘work_years’,‘work_company’,‘work_position’,‘points’,‘click_nums’,‘fav_nums’]
list_filter = [‘org’,‘name’,‘work_years’,‘work_company’,‘work_position’,‘points’,‘click_nums’,‘fav_nums’,‘add_time’]

xadmin.site.register(CityDict,CityDictAdmin)
xadmin.site.register(CourseOrg,CourseOrgAdmin)
xadmin.site.register(Teacher,TeacherAdmin)

  1. 然后再make下创建数据库脚本,执行migrate
  2. Django+xadmin打造在线教育平台(从0-1全过程手把手教程附加各种采坑笔记第5章)_第4张图片
  3. operation.models.py
    ForeignKey----外键
from datetime import datetime

from django.db import models

# Create your models here.
from courses.models import Course
from users.models import UserProfile



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(default=datetime.now,verbose_name=u'添加时间')

    class Meta:
        verbose_name = u'用户咨询'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class CourseComments(models.Model):
    user = models.ForeignKey(UserProfile,verbose_name=u'用户',on_delete=models.CASCADE)
    course = models.ForeignKey(Course,verbose_name=u'课程',on_delete=models.CASCADE)
    comments = models.CharField(max_length=200,verbose_name=u'评论')
    add_time = models.DateTimeField(default=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'用户',on_delete=models.CASCADE)
    fav_id = models.IntegerField(default=0,verbose_name=u'数据id')
    fav_type = models.IntegerField(choices=((1,'课程'),(2,'课程机构'),(3,'讲师')),default=1,verbose_name=u'收藏类型')
    add_time = models.DateTimeField(default=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(default=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'用户',on_delete=models.CASCADE)
    course = models.ForeignKey(Course,verbose_name=u'课程',on_delete=models.CASCADE)
    add_time = models.DateTimeField(default=datetime.now,verbose_name=u'添加时间')

    class Meta:
        verbose_name = u'用户课程'
        verbose_name_plural = verbose_name
  1. courses.models.py
  2. `from datetime import datetime

from django.db import models

Create your models here.

from organization.models import CourseOrg, Teacher

class Course(models.Model):
course_org = models.ForeignKey(CourseOrg,verbose_name=u’课程机构’,null=True,blank=True,on_delete=models.CASCADE)
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(verbose_name=u’难度’,choices=((‘cj’,‘初级’),(‘zj’,‘中级’),(‘gj’,‘高级’)),max_length=2)
learn_times = models.IntegerField(default=0,verbose_name=u’学习时长(分钟)’)
teacher = models.ForeignKey(Teacher,verbose_name=u’讲师’,null=True,blank=True,on_delete=models.CASCADE)
students = models.IntegerField(default=0,verbose_name=u’学习人数’)
fav_nums = models.IntegerField(default=0,verbose_name=u’收藏人数’)
image = models.ImageField(upload_to=‘courses/%Y/%m’,verbose_name=u’封面’,default=’’)
click_nums = models.IntegerField(default=0, verbose_name=u’点击数’)
category = models.CharField(max_length=20,verbose_name=u’课程类别’,default=u’后端开发’)
tag = models.CharField(default=’’,verbose_name=u’课程标签’,max_length=10)
add_time = models.DateTimeField(default=datetime.now,verbose_name=u’添加时间’)
youneed_know = models.CharField(default=’’,max_length=300,verbose_name=u’课程须知’)
teacher_tell = models.CharField(default=’’,max_length=300,verbose_name=u’老师告诉你’)
is_banner = models.BooleanField(default=False,verbose_name=u’是否轮播’)

class Meta:
    verbose_name = u'课程'
    verbose_name_plural = verbose_name


def __str__(self):
    return self.name

class BannerCourse(Course):
#继承Course,不用重新再建立一张表,因为django的xadmin后台使用xadmin.site.register注册时,一张表只能注册一次,在后面页面上只能显示出一个页面。

'''显示轮播课程'''
class Meta:
    verbose_name = '轮播课程'
    verbose_name_plural = verbose_name
    #这里必须设置proxy=True,这样就不会在生成一张表,而且具有Model的功能
    proxy = True

class Lesson(models.Model):
course = models.ForeignKey(Course,verbose_name=u’课程’,on_delete=models.CASCADE)
name = models.CharField(max_length=100,verbose_name=u’章节名’)
learn_times = 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

def get_lesson_video(self):
    #获取章节视频
    return self.video_set.all()

def __str__(self):
    return self.name

class Video(models.Model):
lesson = models.ForeignKey(Lesson,verbose_name=u’章节’,on_delete=models.CASCADE)
name = models.CharField(max_length=100,verbose_name=u’视频名’)
url = models.CharField(max_length=300,verbose_name=u’视频地址’,default=’’)

add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')

class Meta:
    verbose_name = u'视频'
    verbose_name_plural = verbose_name

def __str__(self):
    return self.name

class CourseResourse(models.Model):
course = models.ForeignKey(Course,verbose_name=u’课程’,on_delete=models.CASCADE)
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`
  • Django+xadmin打造在线教育平台(从0-1全过程手把手教程附加各种采坑笔记第5章)_第5张图片
    只有就完成了后台的配置,下一章就是做前端的展示。

你可能感兴趣的:(Django+xadmin打造在线教育平台(从0-1全过程手把手教程附加各种采坑笔记第5章))