新建虛擬環境:mkvirtualenv mxonlinepython
查看已安裝:pip listmysql
django安裝:pip install django==1.9sql
django離線安裝:下載個django壓縮包,workon XX 進入虛擬環境,進入解壓包執行 python setup.py install數據庫
安裝mysql驅動:pip install MySQL_python-1.2.5-cp27-none-win_amd64.whldjango
虛擬環境位置 C:\Users\Aaron\Envs\mxonline\Scripts\python.exesession
數據庫設置app
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 }
新建數據庫less
生成django默認表ide
首先執行:Tools > Run manage.py Task 學習
makemigrations
migrate
啓動服務並訪問
新建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
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
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
新建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'))