具体详细步骤请查看之前文章,这里不进行详细讲解;
1.新建django文件:
File->New Project->Django->Create;
2.安装mysqlclient插件
3.创建数据库
打开navicat for MySQL软件,在已经创建完成的testjango下创建一个命名为**“mxonline”**字符集为“utf8-utf8 unicode”,排序规则“utf8_croatian_ic”的数据表
4.修改settings.py程序连接数据库建立数据表:
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':"mxonline",
"USER":'root',
'PASSWORD':'123456',
'HOST':'127.0.0.1'
}
}
打开Tools->run manage.py Task,调出命令行,在命令行中输入:makemigrations,再输入migrate
5.完善工程文件组成:
打开Tools->run manage.py Task,调出命令行,
在命令行中输入:startapp users,
在命令行中输入:startapp organization,
在命令行中输入:startapp operation,
在命令行中输入:startapp courses,
在model输入的程序:
USER
# _*_ encoding:utf-8 _*_
from __future__ import unicode_literals
from datetime import datetime
from django.db import models
from django.contrib.auth.models import AbstractBaseUser
# Create your models here.
class UserProfile(AbstractBaseUser):
identifier = models.CharField(max_length=40, unique=True)
USERNAME_FIELD = 'identifier'
nick_name = models.CharField(max_length=50,verbose_name=u"昵称",default=" ")
birday = models.DateField(verbose_name=u"生日",null=True,blank=True)
gender = models.CharField(max_length=5,choices=(("male",u"男"),("female","女")),default="female")
address = models.CharField(max_length=100,default=u"")
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):
code = models.CharField(max_length=20,verbose_name=u"验证码")
email =models.EmailField(max_length=5 ,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"轮播图")
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
organization
# _*_ encoding:utf-8 _*_
from __future__ import unicode_literals
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.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 CourseOrg(models.Model):
name = models.CharField(max_length=50,verbose_name= u"机构名称")
desc = models.TextField(verbose_name= u"机构描述")
clik_num = 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"封面图",max_length=100)
address = models.CharField(max_length=150,verbose_name= u"机构地址")
city = models.ForeignKey(CityDict,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
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"教学特点")
clik_num = models.IntegerField(default=0, verbose_name=u"点击数")
fav_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
operation
# _*_ encoding: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
# Create your models here.
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
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
courses
# _*_ encoding:utf-8 _*_
from __future__ import unicode_literals
from datetime import datetime
from django.db import models
# Create your models here.
class Course(models.Model):
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","初级"),("zj","中级"),("gj","高级")),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="courses/%Y/%m",verbose_name=u"封面图",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"课程",on_delete=models.CASCADE,)
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"章节",on_delete=models.CASCADE,)
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"课程",on_delete=models.CASCADE,)
name = models.CharField(max_length=100,verbose_name=u"课程名")
download = models.FileField(upload_to="course/resource/%Y/%m",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
注意头像,需要安装:pillow插件
5.1修改setting里内容,添加各个新建的app
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
"courses",
"organization",
"operation",
]
AUTH_USER_MODEL = "users.UserProfile"
5.2将model里面的表格更新到数据库中:
打开Tools->run manage.py Task,调出命令行,
在命令行中输入:makemigrations ,再输入migrate ;
6.在使用操作过程中遇到的问题:
1.AttributeError: type object ‘UserProfile’ has no attribute ‘USERNAME_FIELD’ models.py
问题解决https://blog.csdn.net/qq_41068712/article/details/89028993