2018-10-08django仿慕课网(一)

2018-10-08  本文已影响0人  afreez

新建项目
1、虚拟环境

  mkvirtualenv xconline

  pip install django==1.9

2、pycharm新建project

3、安装mysql windows驱动

(xconline) G:\迅雷下载>pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl

4、xconline/xconline/settings.py

          DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': "xconline",
    'USER': 'root',
    'PASSWORD': '123456',
    'HOST': "127.0.01"
}

}
5、在Navicat中新建数据库xconline
6、pycharm/tool/run manage.py Task

  manage.py@xconline > makemigrations
  manage.py@xconline > migrate

7、pycharm/run/debug

新建app:
1、pycharm/tool/run manage.py Task
manage.py@xconline > startapp users

2、自动生成了users目录,xconline/users/models.py

    # _*_ encoding:utf-8 _*_
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.

class UserProfile(AbstractUser):
    nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default="")
    birthday = models.DateField(verbose_name=u"生日", null=True, blank=True)
    gender = models.CharField(max_length=5, choices=(("male",u"男"), ("female",u"女")), default="female")
    address = models.CharField(max_length=100, verbose_name=u"邮箱", default=u"")
    mobile = models.CharField(max_length=11, null=True, verbose_name=u"手机号", blank=True)
    image =models.ImageField(upload_to="image/%Y/%m",default=u"image/default.png", max_length=100)

    class Meta:
        verbose_name= "用户信息"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.username
       

3、settings中

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'users',
]
AUTH_USER_MODEL = "users.UserProfile"

4、pycharm/tool/run manage.py Task

manage.py@xconline > makemigrations users
manage.py@xconline > migrate users

自定义userprofile覆盖默认user表
EmailVerifyRecord 邮箱验证码
pagebanner-轮播图也都放在users app中
5、以相同的方法,生成courses,organization,operation三个app和各自的表

/courses/models.py
# _*_ 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"课程")
    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"章节")
    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"课程")
    name = models.CharField(max_length=100, verbose_name=u"名称")
    download = models.FileField(upload_to="course/resource/%Y/%m", verbose_name=u"资源文件", max_length=100)
    add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")

    class Meta:
        verbose_name = u"课程资源"
        verbose_name_plural = verbose_name




上一篇下一篇

猜你喜欢

热点阅读