3 项目开发 - 数据库
1 环境安装
-
新建虚拟环境:mkvirtualenv mxonline
-
查看已安装:pip list
-
django安装:pip install django==1.9
-
django离线安装:下载个django压缩包,workon XX 进入虚拟环境,进入解压包执行 python setup.py install
-
安装mysql驱动:pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
2 新建项目
-
虚拟环境位置 C:\Users\Aaron\Envs\mxonline\Scripts\python.exe
-
数据库设置
1 DATABASES = { 2 'default': { 3 'ENGINE': 'django.db.backends.mysql', 4 'NAME': 'XX', 5 'USER': 'root', 6 'PASSWORD': '', 7 'HOST': '127.0.0.1' 8 } 9 }
-
新建数据库
-
生成django默认表
-
首先执行:Tools > Run manage.py Task
-
makemigrations
-
migrate
-
启动服务并访问
3 新建APP
-
新建app
-
首先执行:Tools > Run manage.py Task
-
startapp users
-
编写users的models
1 #_*_ encoding:utf-8 _*_ 2 from __future__ import unicode_literals 3 from datetime import datetime 4 5 from django.db import models 6 from django.contrib.auth.models import AbstractUser 7 8 9 class UserProfile(AbstractUser): 10 nick_name = models.CharField(max_length=50, verbose_name=u'昵称', default='') 11 birday = models.DateField(verbose_name=u'生日', null=True, blank=True) 12 gender = models.CharField(max_length=6, choices=(('male', u'男'), ('female', u'女')), default='female') 13 address = models.CharField(max_length=100, default=u'') 14 mobile = models.CharField(max_length=11, null=True, blank=True) 15 image = models.ImageField(upload_to='image/%Y/%m', default=u'image/default.png', max_length=100) 16 17 class Meta: 18 verbose_name = u'用户信息' 19 verbose_name_plural = verbose_name 20 21 def __unicode__(self): 22 return self.username 23 24 25 class EmailVerifyRecord(models.Model): 26 code = models.CharField(max_length=20, verbose_name=u'验证码') 27 email = models.EmailField(max_length=50, verbose_name=u'邮箱') 28 send_type = models.CharField(max_length=10, choices=(('register', u'注册'), ('forget', u'忘记密码'))) 29 send_time = models.DateTimeField(default=datetime.now) 30 31 class Meta: 32 verbose_name = u'邮箱验证码' 33 verbose_name_plural = verbose_name 34 35 36 class Banner(models.Model): 37 title = models.CharField(max_length=100, verbose_name=u'标题') 38 image = models.ImageField(max_length=100, upload_to='banner/%Y/%m', verbose_name=u'轮播图') 39 url = models.URLField(max_length=200, verbose_name=u'访问地址') 40 index = models.IntegerField(default=100, verbose_name=u'顺序') 41 add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') 42 43 class Meta: 44 verbose_name = u'轮播图' 45 verbose_name_plural = verbose_name
- 注册app
1 INSTALLED_APPS = [ 2 'django.contrib.admin', 3 'django.contrib.auth', 4 'django.contrib.contenttypes', 5 'django.contrib.sessions', 6 'django.contrib.messages', 7 'django.contrib.staticfiles', 8 'users' 9 ]
-
重载一个setting的方法
1 AUTH_USER_MODEL = 'users.UserProfile'
-
安装Pillow
-
错误信息:users.UserProfile.image: (fields.E210) Cannot use ImageField because Pillow is not installed.
-
安装:pip install Pillow
-
生成users app的表
-
首先执行:Tools > Run manage.py Task
-
makemigrations users
-
migrate users
-
新建app
-
首先执行:Tools > Run manage.py Task
-
startapp courses
-
编写courses的models
1 #_*_ encoding:utf-8 _*_ 2 from __future__ import unicode_literals 3 from datetime import datetime 4 5 from django.db import models 6 7 8 class Course(models.Model): 9 name = models.CharField(max_length=50, verbose_name=u'课程名称') 10 desc = models.CharField(max_length=300, verbose_name=u'课程描述') 11 detail = models.TextField(verbose_name=u'课程详情') 12 degree = models.CharField(max_length=2, choices=(('cj','初级'), ('zj','中级'), ('gj','高级'))) 13 learn_times = models.IntegerField(default=0, verbose_name=u'学习时长(分钟)') 14 students = models.IntegerField(default=0, verbose_name=u'学习人数') 15 fav_nums = models.IntegerField(default=0, verbose_name=u'收藏人数') 16 image = models.ImageField(max_length=100, upload_to='courses/%Y/%m', verbose_name=u'封面图') 17 click_nums = models.IntegerField(default=0, verbose_name=u'点击数') 18 add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') 19 20 class Meta: 21 verbose_name = u'课程' 22 verbose_name_plural = verbose_name 23 24 25 class Lesson(models.Model): 26 course = models.ForeignKey(Course, verbose_name=u'课程') 27 name = models.CharField(max_length=100, verbose_name=u'章节名') 28 add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') 29 30 class Meta: 31 verbose_name = u'章节' 32 verbose_name_plural = verbose_name 33 34 35 class Video(models.Model): 36 lesson = models.ForeignKey(Course, verbose_name=u'章节') 37 name = models.CharField(max_length=100, verbose_name=u'视频名') 38 add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') 39 40 class Meta: 41 verbose_name = u'视频' 42 verbose_name_plural = verbose_name 43 44 45 class CourseResource(models.Model): 46 course = models.ForeignKey(Course, verbose_name=u'课程') 47 name = models.CharField(max_length=100, verbose_name=u'资源名称') 48 download = models.FileField(max_length=100, upload_to='course/resource/%Y/%m', verbose_name=u'资源文件') 49 add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') 50 51 class Meta: 52 verbose_name = u'课程资源' 53 verbose_name_plural = verbose_name
-
新建app
-
首先执行:Tools > Run manage.py Task
-
startapp organization
-
编写organization的models
1 #_*_ encoding:utf-8 _*_ 2 from __future__ import unicode_literals 3 from datetime import datetime 4 5 from django.db import models 6 7 8 class CityDict(models.Model): 9 name = models.CharField(max_length=20, verbose_name=u'城市') 10 desc = models.CharField(max_length=200, verbose_name=u'描述') 11 add_time = models.DateTimeField(default=datetime.now) 12 13 class Meta: 14 verbose_name = u'城市' 15 verbose_name_plural = verbose_name 16 17 18 class CourseOrg(models.Model): 19 name = models.CharField(max_length=50, verbose_name=u'机构名称') 20 desc = models.TextField(verbose_name=u'机构描述') 21 click_nums = models.IntegerField(default=0, verbose_name=u'点击数') 22 fav_nums = models.IntegerField(default=0, verbose_name=u'收藏数') 23 image = models.ImageField(upload_to='org/%Y/%m', verbose_name=u'封面图片') 24 address = models.CharField(max_length=150, verbose_name=u'机构地址') 25 city = models.ForeignKey(CityDict, verbose_name=u'所在城市') 26 add_time = models.DateTimeField(default=datetime.now) 27 28 class Meta: 29 verbose_name = u'课程机构' 30 verbose_name_plural = verbose_name 31 32 33 class Teacher(models.Model): 34 org = models.ForeignKey(CourseOrg, verbose_name=u'所属机构') 35 name = models.CharField(max_length=50, verbose_name=u'教师名') 36 work_years = models.IntegerField(default=0, verbose_name=u'工作年限') 37 work_company = models.CharField(max_length=50, verbose_name=u'就职公司') 38 points = models.CharField(max_length=50, verbose_name=u'教学特点') 39 click_nums = models.IntegerField(default=0, verbose_name=u'点击数') 40 fav_nums = models.IntegerField(default=0, verbose_name=u'收藏数') 41 add_time = models.DateTimeField(default=datetime.now) 42 43 class Meta: 44 verbose_name = u'教师' 45 verbose_name_plural = verbose_name
-
新建app
-
首先执行:Tools > Run manage.py Task
-
startapp operation
-
编写operation的models
1 #_*_ encoding:utf-8 _*_ 2 from __future__ import unicode_literals 3 from datetime import datetime 4 5 from django.db import models 6 7 from users.models import UserProfile 8 from courses.models import Course 9 10 11 class UserAsk(models.Model): 12 name = models.CharField(max_length=20, verbose_name=u'姓名') 13 mobile = models.CharField(max_length=11, verbose_name=u'手机') 14 course_name = models.CharField(max_length=50, verbose_name=u'课程名') 15 add_time = models.DateTimeField(datetime.now, verbose_name=u'添加时间') 16 17 class Meta: 18 verbose_name = u'用户咨询' 19 verbose_name_plural = verbose_name 20 21 22 class CourseComments(models.Model): 23 user = models.ForeignKey(UserProfile, verbose_name=u'用户') 24 course = models.ForeignKey(Course, verbose_name=u'课程') 25 comments = models.CharField(max_length=200, verbose_name=u'评论') 26 add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') 27 28 class Meta: 29 verbose_name =u'课程评论' 30 verbose_name_plural = verbose_name 31 32 33 class UserFavorite(models.Model): 34 user = models.ForeignKey(UserProfile, verbose_name=u'用户') 35 fav_id = models.IntegerField(default=0, verbose_name=u'数据id') 36 fav_type = models.IntegerField(default=0, choices=((1,'课程'), (2,'课程机构'), (3,'讲师')), verbose_name=u'收藏类型') 37 add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') 38 39 class Meta: 40 verbose_name =u'用户收藏' 41 verbose_name_plural = verbose_name 42 43 44 class UserMessage(models.Model): 45 user = models.IntegerField(default=0, verbose_name=u'接收用户') 46 messae = models.CharField(max_length=500, verbose_name=u'消息内容') 47 has_read = models.BooleanField(default=False, verbose_name=u'是否已读') 48 add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') 49 50 class Meta: 51 verbose_name =u'用户消息' 52 verbose_name_plural = verbose_name 53 54 55 class UserCourse(models.Model): 56 user = models.ForeignKey(UserProfile, verbose_name=u'用户') 57 course = models.ForeignKey(Course, verbose_name=u'课程') 58 add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') 59 60 class Meta: 61 verbose_name =u'用户课程' 62 verbose_name_plural = verbose_name
- 注册app
1 INSTALLED_APPS = [ 2 'django.contrib.admin', 3 'django.contrib.auth', 4 'django.contrib.contenttypes', 5 'django.contrib.sessions', 6 'django.contrib.messages', 7 'django.contrib.staticfiles', 8 'users', 9 'course', 10 'organization', 11 'operation' 12 ]
-
生成数据表
-
执行命令:makemigrations
-
执行命令:migrate
4 合并APPS
-
新建apps
新建python package,名为apps
-
Mark Directory as > Source Root
-
配置setting文件,把apps文件夹加入到python的搜索路径下
1 import os 2 import sys 3 # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 4 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 5 sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))