标签:图片 pre datetime migrate 接受 banner _id detail learn
创建工程与创建相应的app
django-admin startproject MxOnline
python3 manage.py startapp users
python3 manage.py startapp course
python3 manage.py startapp organization
python3 manage.py startapp operation
对于user中,由于userprofile不能够完全满足我们的需求,因此。自定义userprofile
安装:pillow,pymysql「用来处理图片,连接数据库」
创建users/model
包括:用户的个人信息,邮箱与轮播图
from django.db import models from django.contrib.auth.models import AbstractUser from datetime import datetime 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) #verbose_name指定在admin管理界面中显示中文; # verbose_name表示单数形式的显示,verbose_name_plural表示复数形式的显示;中文的单数和复数一般不作区别。 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
重载AUTH_USER_MODEL
AUTH_USER_MODEL = ‘users.UserProfile‘
设计数据库为Mysql
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.mysql‘,
‘NAME‘: ‘mxonline‘, #数据库名字
‘USER‘: ‘root‘, #账号
‘PASSWORD‘: ‘123456‘, #密码
‘HOST‘: ‘127.0.0.1‘, #IP
‘PORT‘: ‘3306‘, #端口
}
}
init.py里面导入pymysql模块
# user/__init__.py
import pymysql
pymysql.install_as_MySQLdb()
课程:
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
教育机构
from django.db import models from datetime import datetime 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 class CourseOrg(models.Model): name = models.CharField(‘机构名称‘,max_length=50) desc = models.TextField(‘机构描述‘) click_nums = models.IntegerField(‘点击数‘,default=0) fav_nums = models.IntegerField(‘收藏数‘,default=0) image = models.ImageField(‘封面图‘,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 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) 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)
操作:
from datetime import datetime from django.db import models from course.models import Course from 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
到settings的“INSTALLED_APPS”里面
INSTALLED_APPS = [
‘django.contrib.admin‘,
‘django.contrib.auth‘,
‘django.contrib.contenttypes‘,
‘django.contrib.sessions‘,
‘django.contrib.messages‘,
‘django.contrib.staticfiles‘,
‘users‘,
‘course‘,
‘organization‘,
‘operation‘,
]
迁移数据库
python3 manage.py makemigrations
python3 manage.py migrate
创建一个名为apps的包,并将所有的app移动到里面去。
最后启动即可:sudo python3 manage.py runserver
标签:图片 pre datetime migrate 接受 banner _id detail learn
原文地址:https://www.cnblogs.com/topass123/p/12930241.html