先在课程的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 %}
全部课程
查看更多 >
{% for course in all_courses %}
{% 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\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 %}
{% endfor %}
图片链接这样配置
{{ MEDIA_URL }}{{ course.image }}]
然后配置讲师页。先上传讲师头像,增加个image字段,makemigrations, migrate.然后在org-detail-homepage里显示教师的部分做成for循环,
{% for teacher in all_teachers %}
昵称:{{ teacher.name }}
java开发教程
发表于:2015-10-12
课程介绍
{% endfor %}
然后是机构课程列表页org-detail-course
{% extends 'org_base.html' %}
{% block title %}机构课程列表页{% endblock %}
{% block right_form %}
{% endblock %}
urls.py
url(r'^course/(?P\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里