#urls.py
url(r'^courses/', include('courses.url', namespace='courses')), # organiztion 的url分发,namespace用于重名的处理
#app.url.py
urlpatterns = [
url(r'^courses_list/$', CoursesView.as_view(), name='courses_list'),
]
class CoursesView(View):
def get(self,request):
return render(request, 'course_list.html',{})
all_course = Course.objects.all()
return render(request, 'course_list.html',{'all_course':all_course})
{% for course in all_course %}
{% endfor %}
class CoursesView(View):
def get(self,request):
all_course = Course.objects.all()
###############################分页功能###################################
try:
page = request.GET.get('page', 1)
except PageNotAnInteger:
page = 1
# Provide Paginator with the request object for complete querystring generation
p = Paginator(all_course, 2, request=request)
course = p.page(page)
##########################################################################
return render(request, 'course_list.html',{'all_course':course})
{# 页码 #}
{% if all_course.has_previous %} {# 判断是否有前一页逻辑 #}
- 上一页
{% endif %}
{% for page in all_course.pages %}
{% if page %}
{% ifequal page all_course.number %} {# 判断是否是当前页 #}
- {{ page }}
{% else %} {# 如果不是当前页的话 #}
- {{ page }}
{% endifequal %}
{% endif %}
{% endfor %}
{% if all_course.has_next %} {# 判断是否有后一页逻辑 #}
- 下一页
{% endif %}
class CoursesView(View):
def get(self,request):
if request.method == 'GET':
sort = request.GET.get('sort', '')
if sort == 'last':
all_course = Course.objects.all().order_by('-AddTime')
elif sort == 'hot':
all_course = Course.objects.all().order_by('-Students')
elif sort == 'student':
all_course = Course.objects.all().order_by('-ClikeNumber')
else:
all_course = Course.objects.all()
###############################分页功能###################################
try:
page = request.GET.get('page', 1)
except PageNotAnInteger:
page = 1
# Provide Paginator with the request object for complete querystring generation
p = Paginator(all_course, 3, request=request)
course = p.page(page)
##########################################################################
return render(request, 'course_list.html',{'all_course':course,'sort':sort})
hot_course = Course.objects.all().order_by('-FavouriteNumber')[:3]
热门课程推荐
{% for course in hot_course %}
-
-
{{ course.CourseName }}
{# 利用get_model_display用于显示choice的选项代表值 #}
难度:{{ course.get_degree_display }}
{% endfor %}
url(r'^courses_detail/(?P\d+)/$', CourseDetailView.as_view(), name='course_detail'),
class CourseDetailView(View):
def get(self,request,course_id):
course_detail = Course.objects.get(id=course_id)
return render(request, 'course_detail.html',{'course_detail':course_detail})
{% extends 'organization_base.html' %}
{% load staticfiles %}
{% block title %}公开课详情{% endblock %}
{% block header %}填入对应内容{% endblock %}
{% block custom_bread %}填入对应内容{% endblock %}
{% block content %}填入对应内容{% endblock %}
{% block custom_js %}填入对应内容{% endblock %}
#Lesson
class Lesson(models.Model):
LessonName = models.CharField(max_length=100,verbose_name=u'章节名称')
Course = models.ForeignKey(Course,verbose_name=u'课程名称')
AddTime = models.DateTimeField(default=datetime.now,verbose_name=u'添加时间')
class Meta:
verbose_name = u'章节'
verbose_name_plural = verbose_name
def __str__(self):
return '{0}'.format(self.LessonName)
#Course
class Course(models.Model):
Organization = models.ForeignKey(CourseOrganization,verbose_name=u'所属机构',null=True,blank=True)
Describe = models.CharField(verbose_name=u'课程简介',max_length=500)
CourseName = models.CharField(max_length=100,verbose_name=u'课程名称')
Detail = models.TextField(verbose_name=u'课程详情')
degree = models.CharField(max_length=10,verbose_name=u'课程难度',choices=(('cj',u'初级'),('zj',u'中级'),('gj',u'高级')))
LearnTime = models.IntegerField(verbose_name=u'学习时长',default=0)
# Lesson = models.IntegerField(verbose_name=u'章节数',default=0)
CourseType = models.CharField(max_length=30,verbose_name=u'课程类型')
#LearnUser = models.ImageField(upload_to='static/Pic/courses/%Y/%m',max_length=500,verbose_name=u'学习用户')
CoursePicture = models.ImageField(upload_to='courses/%Y/%m',max_length=500,verbose_name=u'课程封面')
Students = models.IntegerField(default=0,verbose_name=u'学习人数')
FavouriteNumber = models.IntegerField(default=0,verbose_name=u'喜欢人数')
ClikeNumber = models.IntegerField(default=0,verbose_name=u'点击人数')
AddTime = models.DateTimeField(default=datetime.now,verbose_name=u'添加时间',)
def __str__(self):
return '{0}'.format(self.CourseName)
def get_lesson_nums(self):
"""
course作为lesson的外键,利用self.lesson_set获取lesson参数
"""
lesson_num = self.lesson_set.all().count()
return lesson_num
class Meta:
verbose_name = u'课程信息'
verbose_name_plural = verbose_name
#UserCourse,内包含User和Course两个外键,是将两个数据进行关联
class UserCourse(models.Model):
User = models.ForeignKey(UserProfile,verbose_name=u'用户')
Course = models.ForeignKey(Course,verbose_name=u'课程')
AddTime = models.DateTimeField(default=datetime.now,verbose_name=u'添加时间')
class Meta:
verbose_name = u'用户课程'
verbose_name_plural =verbose_name
def get_study_user(self):
study_user = self.usercourse_set.all()[:5]
return study_user
{% for study_user in course_detail.get_study_user %}
{% endfor %}
1.给model加上tag的参数,makemigration -> migrate
Tag = models.CharField(default='',max_length=10,verbose_name=u'所属机构',null=True,blank=True)
2.获取该课程下的tag参数,若有tag,利用获取的tag进行查询,将相同tag作为推荐课程返回模板中
class CourseDetailView(View):
def get(self,request,course_id):
course_detail = Course.objects.get(id=course_id)
#每次单击进入课程详情页面都会触发一次点击数
course_detail.ClikeNumber+=1
course_detail.save()
#查询所有tag,获取推荐课程
tag = course_detail.Tag
if tag: #如果有打标签
relate_course = Course.objects.filter(Tag=tag).filter(~Q(id= int(course_id)))[:1]
# else:
# relate_course = [] #怎么都要传回一个数组,如果传回模板是空的话,会取不到值,会报错
return render(request, 'course_detail.html',{'course_detail':course_detail,'relate_course':relate_course})
1.Django filter符合表示”不等于”
2.filter要用for遍历,get不能遍历
3.多次过滤直接加多几次filter
使用了xadmin之后,会在相应app下将model注册进xadmin,若model下的某些参数已经删除,但是在注册进xadmin下未删除,则会出现报错‘FieldDoesNotExist’
class CourseAdmin(object):
list_display = ['CourseName','degree','Lesson','CourseType','Students','FavouriteNumber','ClikeNumber','AddTime']
search_fields = ['CourseName','degree','Lesson','CourseType','Students','FavouriteNumber','ClikeNumber']
list_filter = ['CourseName','degree','Lesson''CourseType','Students','FavouriteNumber','ClikeNumber','AddTime']
#利用 admin.site.register 进行注册
xadmin.site.register(Course,CourseAdmin)
解决方法:
若model下的某些参数已经删除了,相应的将注册进xadmin的参数也删除
class CourseAdmin(object):
list_display = ['CourseName','degree','CourseType','Students','FavouriteNumber','ClikeNumber','AddTime']
search_fields = ['CourseName','degree','CourseType','Students','FavouriteNumber','ClikeNumber']
list_filter = ['CourseName','degree','CourseType','Students','FavouriteNumber','ClikeNumber','AddTime']
#利用 admin.site.register 进行注册
xadmin.site.register(Course,CourseAdmin)
class Video(models.Model):
LessonName = models.ForeignKey(Lesson,verbose_name=u'章节名称',default='未命名',)
VideoName = models.CharField(max_length=100, verbose_name=u'视频名称')
AddTime = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')
VideoUrl = models.CharField(default='',verbose_name='播放路径',max_length=200,null=True,blank=True) #播放地址
class Meta:
verbose_name = u'视频'
verbose_name_plural = verbose_name
#model中利用_set函数获取外键信息
class Lesson(models.Model):
LessonName = models.CharField(max_length=100,verbose_name=u'章节名称',default='未命名',null=True,blank=True)
Course = models.ForeignKey(Course,verbose_name=u'课程名称')
AddTime = models.DateTimeField(default=datetime.now,verbose_name=u'添加时间')
class Meta:
verbose_name = u'章节'
verbose_name_plural = verbose_name
def __str__(self):
return '{0}'.format(self.LessonName)
#获取各个章节下的课程video
def get_video(self):
video = self.video_set.all()
return video
class Video(models.Model):
LessonName = models.ForeignKey(Lesson,verbose_name=u'章节名称',default='未命名',)
VideoName = models.CharField(max_length=100, verbose_name=u'视频名称')
AddTime = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')
VideoUrl = models.CharField(default='',verbose_name='播放路径',max_length=200,null=True,blank=True) #播放地址
LearnTime = models.IntegerField(verbose_name=u'学习时长',default=0)
class Meta:
verbose_name = u'视频'
verbose_name_plural = verbose_name
def __str__(self):
return '{0}'.format(self.VideoName)
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/%d',max_length=100, verbose_name=u'下载')
AddTime = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')
class Meta:
verbose_name = u'课程资源'
verbose_name_plural = verbose_name
附件上传利用models.FileField,将路径会上传至media路径下,在模板中要切记添加{{MEDIA_URL}},该可以路由至正确地址进行下载
DownLoad = models.FileField(upload_to='course/resource/%Y/%m/%d',max_length=100, verbose_name=u'下载')
#课程通过筛选项对数据进行过滤
class CoursesView(View):
def get(self,request):
###############################筛选功能###################################
sort = request.GET.get('sort', '')
if sort == 'last':
all_course = Course.objects.all().order_by('-AddTime')
elif sort == 'hot':
all_course = Course.objects.all().order_by('-Students')
elif sort == 'student':
all_course = Course.objects.all().order_by('-ClikeNumber')
else:
all_course = Course.objects.all()
###############################分页功能###################################
try:
page = request.GET.get('page', 1)
except PageNotAnInteger:
page = 1
# Provide Paginator with the request object for complete querystring generation
p = Paginator(all_course,9, request=request)
course = p.page(page)
###########################热门课程推荐################################
hot_course = Course.objects.all().order_by('-FavouriteNumber')[:3]
return render(request, 'course_list.html',{'all_course':course,'sort':sort,'hot_course':hot_course})
#通过course_id选择到正确的课程进行信息展示,通过标签的方式,完成课程推荐动作
class CourseDetailView(View):
def get(self,request,course_id):
course_detail = Course.objects.get(id=course_id)
#每次单击进入课程详情页面都会触发一次点击数
course_detail.ClikeNumber+=1
course_detail.save()
#查询所有tag,获取推荐课程
tag = course_detail.Tag
if tag: #如果有打标签
relate_course = Course.objects.filter(Tag=tag).filter(~Q(id= int(course_id)))[:1]
# else:
# relate_course = [] #怎么都要传回一个数组,如果传回模板是空的话,会取不到值,会报错
return render(request, 'course_detail.html',{'course_detail':course_detail,'relate_course':relate_course})
#通过course_id获取课程下的lesson和course_resourse,并将信息返回至模板中通过遍历对信息进行展示
class CourseVideoView(View):
def get(self,request,course_id):
course = Course.objects.get(id=course_id)
lesson = Lesson.objects.filter(Course=course)
course_resource = CourseResource.objects.filter(Course=course)
##########################学过该课程的同学还学过##########################
#利用opreation-UserCourse记录的用户行为
Other_User = UserCourse.objects.filter(Course=course)
Other_User_List = []
for u in Other_User:
Other_User_List.append(u.id)
##########################################################################
return render(request, 'course_video.html',{'course':course,'lesson':lesson,'course_resource':course_resource})
1.绝对地址:在要模板中,要跳转的绝对地址前加上反斜杠“/”
2.相对地址:在要模板中,要跳转的相对地址中,直接加上要访问的地址
3.站外访问:外站需要包含协议(http)域名的,如果省略域名则浏览器会认为是当前域名
#用户行为记录数据,用于绑定记录用户和用户学习过的课程的行为
class UserCourse(models.Model):
User = models.ForeignKey(UserProfile,verbose_name=u'用户')
Course = models.ForeignKey(Course,verbose_name=u'课程')
AddTime = models.DateTimeField(default=datetime.now,verbose_name=u'添加时间')
class Meta:
verbose_name = u'用户课程'
verbose_name_plural =verbose_name
#对学习过该课程的用户进行遍历,获取其他用户还学习过的课程
class CourseVideoView(View):
def get(self,request,course_id):
course = Course.objects.get(id=course_id)
lesson = Lesson.objects.filter(Course=course)
course_resource = CourseResource.objects.filter(Course=course)
##########################学过该课程的同学还学过##########################
#利用opreation-UserCourse记录的用户行为,在用户选择学习后,需要将用户行为存储进入该数据库中
Other_User_List = UserCourse.objects.filter(Course=course) #利用课程数据遍历出所有学习该课程下的用户
Related_Courses = set()
Related_Courses_id = set()
for u in Other_User_List:
user_courses = UserCourse.objects.filter(User_id=u.id) #遍历所有用户,并利用用户id遍历出所有的course,注意起名字不要重复了
for c in user_courses:
if c.Course_id not in Related_Courses_id: #利用课程id进行去重
Related_Courses.add(c)
Related_Courses_id.add(c.Course_id)
##########################################################################
return render(request, 'course_video.html',{'course':course,'lesson':lesson,'course_resource':course_resource,'Related_Courses':Related_Courses,})