課程應用
應用管理
建立應用:apps/course
"""
1.終端前往luffyapi/apps所在目錄
>: python ../../manage.py startapp course
2.settings/dev.py中註冊course
INSTALLED_APPS = [
# ...
'course'
]
3.分發路由
1) 主路由urls.py
from django.urls import include
urlpatterns = [
# ...
path('course/', include('course.urls')),
]
2) 子路由
from django.urls import path, re_path
from . import views
urlpatterns = [
]
4.xadmin顯示配置
1) course/__init__.py
default_app_config = "course.apps.CourseConfig"
#2) course/apps.py
from django.apps import AppConfig
class CourseConfig(AppConfig):
name = 'user'
verbose_name = '免費課程'
"""
數據層設計
建立models:course/models.py
class CourseCategory(BaseModel):
"""分類"""
name = models.CharField(max_length=64, unique=True, verbose_name="分類名稱")
class Meta:
db_table = "luffy_course_category"
verbose_name = "分類"
verbose_name_plural = verbose_name
def __str__(self):
return "%s" % self.name
class Course(BaseModel):
"""課程"""
course_type = (
(0, '付費'),
(1, 'VIP專享'),
(2, '學位課程')
)
level_choices = (
(0, '初級'),
(1, '中級'),
(2, '高級'),
)
status_choices = (
(0, '上線'),
(1, '下線'),
(2, '預上線'),
)
name = models.CharField(max_length=128, verbose_name="課程名稱")
course_img = models.ImageField(upload_to="courses", max_length=255, verbose_name="封面圖片", blank=True, null=True)
course_type = models.SmallIntegerField(choices=course_type, default=0, verbose_name="付費類型")
# 使用這個字段的緣由
brief = models.TextField(max_length=2048, verbose_name="詳情介紹", null=True, blank=True)
level = models.SmallIntegerField(choices=level_choices, default=0, verbose_name="難度等級")
pub_date = models.DateField(verbose_name="發佈日期", auto_now_add=True)
period = models.IntegerField(verbose_name="建議學習週期(day)", default=7)
attachment_path = models.FileField(upload_to="attachment", max_length=128, verbose_name="課件路徑", blank=True,
null=True)
status = models.SmallIntegerField(choices=status_choices, default=0, verbose_name="課程狀態")
course_category = models.ForeignKey("CourseCategory", on_delete=models.SET_NULL, db_constraint=False, null=True, blank=True,
verbose_name="課程分類")
students = models.IntegerField(verbose_name="學習人數", default=0)
sections = models.IntegerField(verbose_name="總課時數量", default=0)
pub_sections = models.IntegerField(verbose_name="課時更新數量", default=0)
price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="課程原價", default=0)
teacher = models.ForeignKey("Teacher", on_delete=models.DO_NOTHING, null=True, blank=True, verbose_name="授課老師")
class Meta:
db_table = "luffy_course"
verbose_name = "課程"
verbose_name_plural = "課程"
def __str__(self):
return "%s" % self.name
class Teacher(BaseModel):
"""導師"""
role_choices = (
(0, '講師'),
(1, '導師'),
(2, '班主任'),
)
name = models.CharField(max_length=32, verbose_name="導師名")
role = models.SmallIntegerField(choices=role_choices, default=0, verbose_name="導師身份")
title = models.CharField(max_length=64, verbose_name="職位、職稱")
signature = models.CharField(max_length=255, verbose_name="導師簽名", help_text="導師簽名", blank=True, null=True)
image = models.ImageField(upload_to="teacher", null=True, verbose_name="導師封面")
brief = models.TextField(max_length=1024, verbose_name="導師描述")
class Meta:
db_table = "luffy_teacher"
verbose_name = "導師"
verbose_name_plural = verbose_name
def __str__(self):
return "%s" % self.name
class CourseChapter(BaseModel):
"""章節"""
course = models.ForeignKey("Course", related_name='coursechapters', on_delete=models.CASCADE, verbose_name="課程名稱")
chapter = models.SmallIntegerField(verbose_name="第幾章", default=1)
name = models.CharField(max_length=128, verbose_name="章節標題")
summary = models.TextField(verbose_name="章節介紹", blank=True, null=True)
pub_date = models.DateField(verbose_name="發佈日期", auto_now_add=True)
class Meta:
db_table = "luffy_course_chapter"
verbose_name = "章節"
verbose_name_plural = verbose_name
def __str__(self):
return "%s:(第%s章)%s" % (self.course, self.chapter, self.name)
class CourseSection(BaseModel):
"""課時"""
section_type_choices = (
(0, '文檔'),
(1, '練習'),
(2, '視頻')
)
chapter = models.ForeignKey("CourseChapter", related_name='coursesections', on_delete=models.CASCADE,
verbose_name="課程章節")
name = models.CharField(max_length=128, verbose_name="課時標題")
orders = models.PositiveSmallIntegerField(verbose_name="課時排序")
section_type = models.SmallIntegerField(default=2, choices=section_type_choices, verbose_name="課時種類")
section_link = models.CharField(max_length=255, blank=True, null=True, verbose_name="課時連接",
help_text="如果video,填vid,如果文檔,填link")
duration = models.CharField(verbose_name="視頻時長", blank=True, null=True, max_length=32) # 僅在前端展現使用
pub_date = models.DateTimeField(verbose_name="發佈時間", auto_now_add=True)
free_trail = models.BooleanField(verbose_name="是否可試看", default=False)
class Meta:
db_table = "luffy_course_Section"
verbose_name = "課時"
verbose_name_plural = verbose_name
def __str__(self):
return "%s-%s" % (self.chapter, self.name)
註冊models:course/adminx.py
import xadmin
from . import models
xadmin.site.register(models.CourseCategory)
xadmin.site.register(models.Course)
xadmin.site.register(models.Teacher)
xadmin.site.register(models.CourseChapter)
xadmin.site.register(models.CourseSection)
數據庫遷移
>: python manage.py makemigrations
>: python manage.py migrate