先在课程的models里给课程建一个外键,
class Course(models.Model):
course_org = models.ForeignKey(CourseOrg,verbose_name="课程机构",null=True)
目的是要在机构详情页显示该机构下的课程。然后写前端html文件,创建org_base.html,改下css、js等路径,block一下。在机构首页org-detail-homepage.html里
{% block right_form %}
<div class="right companycenter layout grouping" >
<div class="head">
<h1>全部课程</h1>
<a class="green fr more" href="org-detail-course.html">查看更多 > </a>
</div>
<div class="brief group_list">
{% for course in all_courses %}
<div class="module1_5 box">
<a href="course-detail.html"><img width="214" src="../media/courses/2016/11/mysql.jpg"/></a>
<div class="des">
<a href="course-detail.html"><h2>django入门</h2></a>
<span class="fl">课时:<i class="key">0</i></span>
<span class="fr">参加人数:3</span>
</div>
<div class="bottom">
<span class="fl">慕课网</span>
<span class="star fr notlogin
" data-favid="13" data-fav-type="4">
0
</span>
</div>
</div>
{% endfor %}
需要知道用户想看哪个机构的主页,于是需要通过url函数传到后台一个id
from django.conf.urls import url, include
from .views import OrgView,AddUserAskView
urlpatterns = [
#课程机构列表页
url(r'^list/$', OrgView.as_view(), name="org_list"),
url(r'^add_ask/$', AddUserAskView.as_view(), name="add_ask"),
url(r'^home/(?P<org_id>\d+)/$', AddUserAskView.as_view(), name="add_ask"),
]
响应函数
class OrgHomeView(View):
"""
机构首页
"""
def get(self,request,org_id):
course_org = CourseOrg.objects.get(id=int(org_id))
all_courses = course_org.course_set.all()[:3]
all_teachers = course_org.teacher_set.all()[:1]
return render(request,'org-detail-homepage.html',{
'all_courses':all_courses,
'all_teachers':all_teachers,
})
这里all_courses = course_org.course_set.all()[:3]的course_set是来自于刚才在course的models里Course类里创建的外键,把Course和CourseOrg连接起来。把Course变成小写,后边加上_set,再.all()就可以调用该CourseOrg实例下的所有course。所有外键都可以这么做。
然后上传image给课程,改html设置
{% for course in all_courses %}
<div class="module1_5 box">
<a href="{% url 'org:org_home' course.id %}"><img width="214" src="{{ MEDIA_URL }}{{ course.image }}"/></a>
<div class="des">
<span class="fl">课时:<i class="key">{{ course.learn_times }}</i></span>
<span class="fr">参加人数:{{ course.students }}</span>
</div>
<div class="bottom">
<span class="fl">{{ course.course_org.name }}</span>
<span class="star fr notlogin
" data-favid="13" data-fav-type="4">
{{ course.fav_nums }}
</span>
</div>
</div>
{% endfor %}
图片链接这样配置
{{ MEDIA_URL }}{{ course.image }}]
然后配置讲师页。先上传讲师头像,增加个image字段,makemigrations, migrate.然后在org-detail-homepage里显示教师的部分做成for循环,
{% 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 %}
然后是机构课程列表页org-detail-course
{% extends 'org_base.html' %}
{% block title %}机构课程列表页{% endblock %}
{% block right_form %}
<div class="right companycenter layout" >
<div class="head">
<h1>机构课程</h1>
</div>
<div class="brief group_list">
{% for course in all_courses %}
<div class="module1_5 box">
<a class="comp-img-box" href="course-detail.html">
<img width="214" height="195" src="{{ MEDIA_URL }}{{ course.image }}"/>
</a>
<div class="des">
<a href="course-detail.html"><h2>{{ course.name }}</h2></a>
<span class="fl">课时:<i class="key">{{ course.learn_times }}</i></span>
<span class="fr">学习人数:{{ course.students }}</span>
</div>
<div class="bottom">
<span class="fl">{{ course.course_org.name }}</span>
<span class="star fr notlogin
" data-favid="13" data-fav-type="4">
{{ course.fav_nums }}
</span>
</div>
</div>
{% endfor %}
</div>
<div class="pageturn">
<ul class="pagelist">
<li class="active"><a href="?page=1">1</a></li>
</ul>
</div>
</div>
{% endblock %}
urls.py
url(r'^course/(?P<org_id>\d+)/$', OrgCourseView.as_view(), name="org_course"),
views.py
class OrgCourseView(View):
"""
机构课程列表页
"""
def get(self,request,org_id):
current_page = 'course'
course_org = CourseOrg.objects.get(id=int(org_id))
all_courses = course_org.course_set.all()
return render(request,'org-detail-course.html',{
'all_courses':all_courses,
'course_org':course_org,
'current_page': current_page
})
为了让网页右侧选中的东西变成active,需要后台传入前端一个变量告诉哪个是active的,'current_page': current_page
org-base里
<div class="wp list personal_list comp">
<div class="left">
<ul>
<li class="{% ifequal current_page 'home' %}active2{% endifequal %}"><a href="{% url 'org:org_home' course_org.id %}">机构首页</a></li>
<li class="{% ifequal current_page 'course' %}active2{% endifequal %}"><a href="{% url 'org:org_course' course_org.id %}">机构课程</a></li>
<li class="{% ifequal current_page 'desc' %}active2{% endifequal %}"><a href="{% url 'org:org_home' course_org.id %}">机构介绍</a></li>
<li class="{% ifequal current_page 'teacher' %}active2{% endifequal %}"><a href="{% url 'org:org_home' course_org.id %}">机构讲师</a></li>
</ul>
</div>
注意class处的写法。
然后是另外两个页面机构介绍和机构讲师。先做HTML文件,讲师页面里需要遍历机构下所有的讲师。然后配置url,再配置views.py。
接下来是面包屑crumbs。在org-base里
<div class="wp">
<ul class="crumbs">
<li><a href="index.html">首页</a>></li>
<li><a href="{% url "org:org_list" %}">课程机构</a>></li>
<li>{% block page_path %}机构首页{% endblock %}</li>
</ul>
</div>
在其他的页面里{% block page_path %}机构介绍{% endblock %}这样改过来。