课程机构首页,如何获取教师的最新课程?

来源:11-2 首页功能开发1

XO鸡丝

2018-01-14

如红框所示.

//img.mukewang.com/szimg/5a5af9f90001e17313310945.jpg

views.py

#机构首页
class OrgHomeView(View):
    def get(self,request,org_id):
        current_page = "home"
        course_org = CourseOrg.objects.get(id=int(org_id))

        # 点击量加1
        course_org.click_nums += 1
        course_org.save()

        all_courses = course_org.course_set.all()[:3]
        all_teachers = course_org.teacher_set.all()[:2]

        # 判断是否已收藏
        has_fav = HasFav(course_org.id,request)

        return render(request,'org-detail-homepage.html',{
            "all_courses": all_courses,
            "all_teachers": all_teachers,
            "course_org": course_org,
            "current_page": current_page,
            "has_fav": has_fav
        })

org-detail-homepage.html

<div class="right companycenter layout" >
        <div class="head">
            <h1>机构教师</h1>
            <a class="green fr more" href="org-detail-teachers.html">查看更多  >  </a>
        </div>
        {% for teacher in all_teachers %}
        <div class="diarys">
            <div class="module5 share company-diary-box" style="padding:10px 0;">
                <div class="left">
                    <img class="pic" src="{{ MEDIA_URL }}{{ teacher.image }}"/>
                    <p>昵称:{{ teacher.name }}</p>
                </div>
                <div class="right">
                    <div class="top">
                        <div class="fl">
                            <a href=""><h1>JAVA开发 </h1></a>
                            <span>发表于:2015-10-12</span>
                        </div>
                    </div>
                    <div class="middle" style="border-bottom:0;">课程介绍</div>
                </div>
            </div>
        </div>
        {% endfor %}

    </div>


如果我在想这一页,获取 教师的最新课程( 就是 <h1>JAVA开发</h1> 那一栏) ,应该怎么写好?

现在我的疑问点是 , 教师的显示是通过. {% for teacher in all_teachers %}

如果我 在后端,在给一个变量, ,那这个变量就不好套进  {% for teacher in all_teachers %}  里面了.(实际上是不懂怎么套)

如果仅通过前端,  那应该如何写呢?

写回答

1回答

XO鸡丝

提问者

2018-01-14

调整了一下代码,用了过滤器,但是感觉我这样的写法,很不高效,-----每个过滤器都会重新查询一次数据库,不能重用其结果.,很浪费资源.

但是如果不这样,用又因为在 {% for %}里,没有好的方法传输单个变量.

SO,老师那边有什么好的建议吗?

<div class="right companycenter layout" >
        <div class="head">
            <h1>机构教师</h1>
            <a class="green fr more" href="org-detail-teachers.html">查看更多  >  </a>
        </div>
        {% for teacher in all_teachers %}
        <div class="diarys">
            <div class="module5 share company-diary-box" style="padding:10px 0;">
                <div class="left">
                    <img class="pic" src="{{ MEDIA_URL }}{{ teacher.image }}"/>
                    <p>昵称:{{ teacher.name }}</p>
                </div>
                <div class="right">
                    <div class="top">
                        <div class="fl">
                            <a href=""><h1>{{ teacher.id|get_teacher_new_class }}</h1></a>
                            <span>发表于:{{ teacher.id|get_teacher_new_class_add_time }}</span>
                        </div>
                    </div>
                    <div class="middle" style="border-bottom:0;">{{ teacher.id|get_teacher_new_class_desc }}</div>
                </div>
            </div>
        </div>
        {% endfor %}

    </div>


organiztions/templatetags/org_filter.py

# -*- coding:utf-8 -*-
from django import template
register = template.Library()

# 根据老师的ID,提取关于这个老师的最新课程
@register.filter
def get_teacher_new_class(key):
    from courses.models import Course
    new_class = Course.objects.filter(teacher=key).order_by('-add_time')[:1]
    if new_class.exists():
        return new_class[0].name
    else:
        return u'暂无课程'

@register.filter
def get_teacher_new_class_add_time(key):
    from courses.models import Course
    new_class = Course.objects.filter(teacher=key).order_by('-add_time')[:1]
    if new_class.exists():
        return new_class[0].add_time
    else:
        return u"ヽ(・ω・。)ノ找不到呢...."

@register.filter
def get_teacher_new_class_desc(key):
    from courses.models import Course
    new_class = Course.objects.filter(teacher=key).order_by('-add_time')[:1]
    if new_class.exists():
        return new_class[0].desc
    else:
        return u"ヽ(*´Д`*)ノ还是找不到呢...."


0
10
bobby
回复
慕莱坞9814221
teacher.course_set.all()
2018-08-20
共10条回复

Python3.6+django+xadmin,打造在线教育平台

【毕设】Python 2.7到3.6 完美适配,Django升级2.0

3677 学习 · 4038 问题

查看课程