CRM开发学员课程系统——我的课程详情页面
在damin里面申请一个角色为学员的账号只能看我的课程界面
1、在student/urls.py文件里添加我的课程的url
#我的课程
url(r'^$',views.stu_my_class,name='stu_my_class'),
我的课程页面:
2、在student/views.py文件中定义一个函数返回课程页面index.html
#student/views.py
def stu_my_class(request):
#enrollment_set通过stu_account里逆向找enrollment_set.all()找到表Enrollment所有数据
print("request.user.stu_account.enrollment_set.all",request.user.stu_account)
return render(request,"student/index.html")
3、处理前端文件student/index.html
需要展示前面的内容,通过用户表里面找到报名表的对象,通过这些一一去找
表需要的关联字段值,算这个学员的这个课程的分数,使用aggregate聚合和Sum去算他的总分数(在student_app中新建一个stu_tags标签)
#student/templatetags/stu_tags.py
# -*- coding:utf-8 -*-
#date: 2019/07/28 23:05
from django import template
from django.utils.safestring import mark_safe
from django.utils.timezone import datetime,timedelta
from django.db.models import Sum
from django.core.exceptions import FieldDoesNotExist
register = template.Library()
@register.simple_tag
def get_score(enroll_obj):
#找到这个用户的课程的所有学习记录,这个学习记录的课程在上课记录里面是有的
study_recore = enroll_obj.studyrecord_set.all().filter(course_record__from_class_id=enroll_obj.enrolled_class.id)
return study_recore.aggregate(Sum('score'))
我的课程详情页面前端:
{% extends 'index.html'%}
{% block page-content %}
{% load stu_tags %}
<div class="panel panel-default">
<div class="panel-body">
Panel content
</div>
<div class="panel panel-default">
<!-- Default panel contents -->
<!-- Table -->
<table class="table">
<thead>
<tr>
<th>课程名称</th>
<th>班级</th>
<th>开课日期</th>
<th>结束日期</th>
<th>课程进度</th>
<th>我的成绩</th>
<th>作业管理</th>
</tr>
</thead>
<tbody>
{% for enroll_obj in request.user.stu_account.enrollment_set.all %}
<tr>
<td>{{enroll_obj.enrolled_class.course}}</td>
<td>s{{enroll_obj.enrolled_class.semester}}</td>
<td>{{enroll_obj.enrolled_class.start_date}}</td>
<td>{{enroll_obj.enrolled_class.end_date}}</td>
<td>已上{{enroll_obj.enrolled_class.courserecord_set.all.count}}节</td>
<td>{% get_score enroll_obj as sum_score%} {{sum_score.score__sum}}</td>
<td><a href="{% url 'mycourse' enroll_obj.id%}">作业管理</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock%}