• django+xadmin 在线教育网站(一)


    win10环境实现

    首先虚拟环境,使用virtualenv 实现
    在这里插入图片描述
    依赖包安装

    diff-match-patch==20121119
    Django==2.1.8
    django-crispy-forms==1.7.0
    django-formtools==2.1
    django-import-export==0.6.1
    django-pure-pagination==0.3.0
    django-ranged-response==0.2.0
    django-reversion==2.0.12
    django-simple-captcha==0.5.6
    et-xmlfile==1.0.1
    future==0.16.0
    httplib2==0.9.2
    jdcal==1.3
    mysqlclient==1.4.6
    odfpy==1.3.6
    openpyxl==2.4.9
    Pillow==5.0.0
    pytz==2017.3
    PyYAML==3.12
    six==1.11.0
    tablib==0.12.1
    unicodecsv==0.14.1
    xlrd==1.1.0
    xlwt==1.3.0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    本人建议,将以上依赖包一次性装好,如果不是的话,有可能会出现pycharm的控制台环境与自己配的环境不同步的情况,出现这种情况,唯一的方法就是删了重建环境

    pycharm 创建Django项目,换上我们自己的虚拟环境
    在这里插入图片描述
    在上方找到‘tool’,点击‘manage.py’,创建四个app

    startapp users
    startapp course
    startapp organization
    startapp operation
    
    • 1
    • 2
    • 3
    • 4

    根目录下创建‘apps’目录,将创建的四个app放进去
    在这里插入图片描述

    各个app的models设置

    # users/models.py
    
    from datetime import datetime
    
    from django.db import models
    from django.contrib.auth.models import AbstractUser
    from django.utils import timezone
    
    class UserProfile(AbstractUser):
    
        gender_choices = (
            ('male','男'),
            ('female','女')
        )
    
        nick_name = models.CharField('昵称',max_length=50,default='')
        birthday = models.DateField('生日',null=True,blank=True)
        gender = models.CharField('性别',max_length=10,choices=gender_choices,default='female')
        adress = 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='image/default.png',max_length=100)
    
        class Meta:
            verbose_name = '用户信息'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.username
    
    
    class EmailVerifyRecord(models.Model):
        send_choices = (
            ('register','注册'),
            ('forget','找回密码')
        )
    
        code = models.CharField('验证码',max_length=20)
        email = models.EmailField('邮箱',max_length=50)
        send_type = models.CharField(choices=send_choices,max_length=10)
        send_time = models.DateTimeField(default=datetime.now)
    
        class Meta:
            verbose_name = '邮箱验证码'
            verbose_name_plural = verbose_name
    
    
    class Banner(models.Model):
        title = models.CharField('标题',max_length=100)
        image = models.ImageField('轮播图',upload_to='banner/%Y%m',max_length=100)
        url = models.URLField('访问地址',max_length=200)
        index = models.IntegerField('顺序',default=100)
        add_time = models.DateTimeField('添加时间',default=datetime.now)
    
        class Meta:
            verbose_name = '轮播图'
            verbose_name_plural = verbose_name
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    # organization/models.py
    
    from datetime import datetime
    
    from django.db import models
    
    
    class CityDict(models.Model):
        name = models.CharField('城市',max_length=20)
        desc = models.CharField('描述',max_length=200)
        add_time = models.DateTimeField(default=datetime.now)
    
        class Meta:
            verbose_name = '城市'
            verbose_name_plural= verbose_name
    
        def __str__(self):
            return self.name
    
    class CourseOrg(models.Model):
        ORG_CHOICES = (
            ("pxjg", u"培训机构"),
            ("gx", u"高校"),
            ("gr", u"个人"),
        )
        name = models.CharField('机构名称',max_length=50)
        desc = models.TextField('机构描述')
        category = models.CharField(max_length=20, choices=ORG_CHOICES, verbose_name=u"机构类别", default="pxjg")
        click_nums = models.IntegerField('点击数',default=0)
        tag = models.CharField('机构标签',max_length=10,default='全国知名')
        fav_nums = models.IntegerField('收藏数',default=0)
        students = models.IntegerField("学习人数",default=0)
        course_nums = models.IntegerField("课程数",default=0)
        image = models.ImageField('logo',upload_to='org/%Y/%m',max_length=100)
        address = models.CharField('机构地址',max_length=150)
        city = models.ForeignKey(CityDict,verbose_name='所在城市',on_delete=models.CASCADE)
        add_time = models.DateTimeField(default=datetime.now)
    
        class Meta:
            verbose_name = '课程机构'
            verbose_name_plural = verbose_name
    
        def get_teacher_nums(self):
            #获取机构的教师数
            return self.teacher_set.all().count()
    
        def __str__(self):
            return self.name
    
    
    class Teacher(models.Model):
        org = models.ForeignKey(CourseOrg,verbose_name='所属机构',on_delete=models.CASCADE)
        name = models.CharField('教师名',max_length=50)
        work_years = models.IntegerField('工作年限',default=0)
        work_company = models.CharField('就职公司',max_length=50)
        work_position = models.CharField('公司职位',max_length=50)
        points = models.CharField('教学特点',max_length=50)
        click_nums = models.IntegerField('点击数',default=0)
        fav_nums = models.IntegerField('收藏数',default=0)
        teacher_age = models.IntegerField('年龄',default=25)
        image = models.ImageField(
            default='',
            upload_to="teacher/%Y/%m",
            verbose_name="头像",
            max_length=100)
        add_time = models.DateTimeField(default=datetime.now)
    
        class Meta:
            verbose_name = '教师'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return "[{0}]的教师: {1}".format(self.org, self.name)
    
        def get_course_nums(self):
            return self.course_set.all().count()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    # operation/models.py
    
    from datetime import datetime
    
    from django.db import models
    
    from apps.course.models import Course
    from apps.users.models import UserProfile
    
    
    class UserAsk(models.Model):
        name = models.CharField('姓名',max_length=20)
        mobile = models.CharField('手机',max_length=11)
        course_name = models.CharField('课程名',max_length=50)
        add_time = models.DateTimeField('添加时间',default=datetime.now)
    
        class Meta:
            verbose_name = '用户咨询'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name
    
    
    class CourseComments(models.Model):
        user = models.ForeignKey(UserProfile,verbose_name='用户',on_delete=models.CASCADE)
        course = models.ForeignKey(Course,verbose_name='课程',on_delete=models.CASCADE)
        comments = models.CharField('评论',max_length=200)
        add_time = models.DateTimeField('添加时间', default=datetime.now)
    
        class Meta:
            verbose_name = '课程评论'
            verbose_name_plural = verbose_name
    
    
    class UserFavorite(models.Model):
        FAV_TYPE = (
            (1,'课程'),
            (2,'课程机构'),
            (3,'讲师')
        )
    
        user = models.ForeignKey(UserProfile,verbose_name='用户',on_delete=models.CASCADE)
        fav_id = models.IntegerField('数据id',default=0)
        fav_type = models.IntegerField(verbose_name='收藏类型',choices=FAV_TYPE,default=1)
        add_time = models.DateTimeField('添加时间', default=datetime.now)
    
        class Meta:
            verbose_name = '用户收藏'
            verbose_name_plural = verbose_name
    
    
    class UserMessage(models.Model):
        user = models.IntegerField('接受用户',default=0)
        message = models.CharField('消息内容',max_length=500)
        has_read = models.BooleanField('是否已读',default=False)
        add_time = models.DateTimeField('添加时间', default=datetime.now)
    
        class Meta:
            verbose_name = '用户消息'
            verbose_name_plural = verbose_name
    
    
    class UserCourse(models.Model):
        user = models.ForeignKey(UserProfile,verbose_name='用户',on_delete=models.CASCADE)
        course = models.ForeignKey(Course,verbose_name='课程',on_delete=models.CASCADE)
        add_time = models.DateTimeField('添加时间', default=datetime.now)
    
        class Meta:
            verbose_name = '用户课程'
            verbose_name_plural = verbose_name
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    # course/models.py
    
    from datetime import datetime
    
    from django.db import models
    
    
    class Course(models.Model):
        DEGREE_CHOICES = (
            ("cj", "初级"),
            ("zj", "中级"),
            ("gj", "高级")
        )
        name = models.CharField("课程名",max_length=50)
        desc = models.CharField("课程描述",max_length=300)
        detail = models.TextField("课程详情")
        degree = models.CharField('难度',choices=DEGREE_CHOICES, max_length=2)
        learn_times = models.IntegerField("学习时长(分钟数)",default=0)
        students = models.IntegerField("学习人数",default=0)
        fav_nums = models.IntegerField("收藏人数",default=0)
        image = models.ImageField("封面图",upload_to="courses/%Y/%m",max_length=100)
        click_nums = models.IntegerField("点击数",default=0)
        add_time = models.DateTimeField("添加时间",default=datetime.now,)
    
        class Meta:
            verbose_name = "课程"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name
    
    class Lesson(models.Model):
        course = models.ForeignKey(Course,verbose_name='课程',on_delete=models.CASCADE)
        name = models.CharField("章节名",max_length=100)
        add_time = models.DateTimeField("添加时间",default=datetime.now)
    
        class Meta:
            verbose_name = "章节"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return '《{0}》课程的章节 >> {1}'.format(self.course, self.name)
    
    class Video(models.Model):
        lesson = models.ForeignKey(Lesson, verbose_name="章节",on_delete=models.CASCADE)
        name = models.CharField("视频名",max_length=100)
        add_time = models.DateTimeField("添加时间", default=datetime.now)
    
        class Meta:
            verbose_name = "视频"
            verbose_name_plural = verbose_name
    
    
    class CourseResource(models.Model):
        course = models.ForeignKey(Course, verbose_name="课程",on_delete=models.CASCADE)
        name = models.CharField("名称",max_length=100)
        download = models.FileField("资源文件",upload_to="course/resource/%Y/%m",max_length=100)
        add_time = models.DateTimeField("添加时间", default=datetime.now)
    
        class Meta:
            verbose_name = "课程资源"
            verbose_name_plural = verbose_name
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    settings文件,配置
    注册app

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'apps.course',
        'apps.operation',
        'apps.organization',
        'apps.users',
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    重载AUTH_USER_MODEL

    AUTH_USER_MODEL = 'users.UserProfile'
    
    • 1

    设计数据库为Mysql

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'imooc',  # 数据库名
            'USER': 'root',  # 数据库账户(root)
            'PASSWORD': '123456',  # 数据库密码
            'HOST': 'localhost',  # 默认为localhost当前主机,如果数据库在远程服务器上,也可以填写目标服务器的ip地址
            'PORT': '3306'  # 默认可以不写
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    更改 编码/时区及设置静态目录

    LANGUAGE_CODE = 'zh-hans'
    
    TIME_ZONE = 'Asia/Shanghai'
    
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    迁移数据库(在上方找到‘tool’,点击‘manage.py’)

    makemigrations
    
    migrate
    
    • 1
    • 2
    • 3

    如果迁移数据库时出现 1,2的选项界面,就是说数据迁移时没有同步,唯一的解决方法就是删库重建,所以本人的建议是将models
    一步整到位,以免出现数据迁移不同步的情况

    之后,运行项目

    在这里插入图片描述

  • 相关阅读:
    基于SSM的校园驿站管理系统
    【类和对象+this引用】
    金仓数据库KingbaseES物理备份恢复命令选项(stanza-upgrade命令)
    数据结构-----排序的概念、常见排序的实现以及排序算法的特点、非比较排序、排序相关例题
    VQA的应用(调研)
    手把手教你配置Linux深度学习服务器-不用本地安装!
    代码整洁之原则
    【Linux】进程等待
    有大量虾皮买家号想防关联该怎么做?
    在真实环境中测试,如何及时停损
  • 原文地址:https://blog.csdn.net/Y57657/article/details/127649089