时间:2023-06-07 10:42:01 | 来源:网站运营
时间:2023-06-07 10:42:01 来源:网站运营
使用django开发一个上线标准的mooc网站(九):完成一个功能的完整步骤:{% extends 'base.html' %}{% block title %}课程讲师 - MOOC在线网{% endblock %}{% load staticfiles %}{% block custom_bread %} <section> <div class="wp"> <ul class="crumbs"> <li><a href="index.html">首页</a>></li> <li>课程讲师</li> </ul> </div> </section>{% endblock %}{% block content %} <section> <div class="wp butler_list_box list"> <div class='left'> <div class="butler_list layout"> <div class="head"> <ul class="fl tab_header"> <li {% if sort == '' %}class="active"{% endif %}><a href="?sort=">全部</a> </li> <li {% if sort == 'hot' %}class="active"{% endif %}><a href="?sort=hot">人气 ↓</a></li> </ul> <div class="fr butler-num">共<span class="key">9</span>人 </div> </div> {% for teacher in all_teachers.object_list %} <dl class="des"> <dt> <a href="{% url 'org:teacher_detail' teacher.id %}"> <div class="picpic"> <img width="100" height="100" class="scrollLoading" src="{{ MEDIA_URL }}{{ teacher.image }}"/> </div> </a> <div class="btn"> <div class="fr btn2 bdsharebuttonbox" data-text="授课教师-奥巴马-慕学在线" data-desc="我在#慕课网#发现了教师“奥巴马”,对学习中的小伙伴很有帮助,一起来看看吧。" data-comment="奥巴马金牌讲师,从业年限:5年" > <span class="fl">分享</span> <a href="#" class="bds_more" data-cmd="more"></a> </div> </div> </dt> <dd> <a href="{% url 'org:teacher_detail' teacher.id %}"> <h1>{{ teacher.name }}<span class="key picbig">金牌讲师</span></h1> </a> <ul class="cont"> <li>工作年限:<span>{{ teacher.work_years }}</span></li> <li>工作职位:<span>{{ teacher.work_position }}</span></li> <li>就职公司:<span>{{ teacher.work_company }} </span></li> <li>年龄:<span>{{ teacher.age }}</span></li> <li>教学特点:<span>{{ teacher.points }}</span></li> </ul> </dd> <a class="buy buyservice" href="/org/teacher/detail/1/"><br/>查看<br/>详情</a> </dl> {% endfor %} </div> <div class="pageturn"> <ul class="pagelist"> {% if all_teachers.has_previous %} <li class="long"><a href="?{{ all_teachers.previous_page_number.querystring }}">上一页</a></li> {% endif %} {% for page in all_teachers.pages %} {% if page %} {% ifequal page all_teachers.number %} <li class="active"><a href="?{{ page.querystring }}">{{ page }}</a></li> {% else %} <li><a href="?{{ page.querystring }}" class="page">{{ page }}</a></li> {% endifequal %} {% else %} <li class="none"><a href="">...</a></li> {% endif %} {% endfor %} {% if all_teachers.has_next %} <li class="long"><a href="?{{ all_teachers.next_page_number.querystring }}">下一页</a></li> {% endif %} </ul> </div> </div> <div class="right layout"> <div class="head">讲师排行榜</div> {% for hot_teacher in sorted_teacher %} <dl class="des"> <span class="num fl">1</span> <a href="/diary/hk_detail/6/"> <dt> <img width="50" height="50" class="scrollLoading" src="{{ MEDIA_URL }}{{ hot_teacher.image }}"/> </dt> </a> <dd> <a href="/diary/hk_detail/6/"> <h1 title="{{ hot_teacher.name }}">{{ hot_teacher.name }}</h1> </a> <p>工作年限:<span>{{ hot_teacher.work_years }}年</span></p> </dd> </dl> {% endfor %} </div> </div></section>{% endblock %}
teacher-detail.html{% extends 'base.html' %}{% block title %}讲师详情 - MOOC在线网{% endblock %}{% load staticfiles %}{% block custom_bread %} <section> <div class="wp"> <div class="crumbs"> <ul> <li><a href="/">首页</a>></li> <li><a href="{% url 'org:teacher_list' %}">授课讲师</a>></li> <li>讲师详情</li> </ul> </div> </div> </section>{% endblock %}{% block custum_js %}<script type="text/javascript">//收藏分享function add_fav(current_elem, fav_id, fav_type){ $.ajax({ cache: false, type: "POST", url:"/org/add_fav/", data:{'fav_id':fav_id, 'fav_type':fav_type}, async: true, beforeSend:function(xhr, settings){ xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}"); }, success: function(data) { if(data.status == 'fail'){ if(data.msg == '用户未登录'){ window.location.href="/login/"; }else{ current_elem.text('收藏') alert(data.msg) } }else if(data.status == 'success'){ current_elem.text(data.msg) } }, });}$('#jsLeftBtn').on('click', function(){ add_fav($(this), 1, 3);});$('#jsRightBtn').on('click', function(){ add_fav($(this), 1, 2);});</script>{% endblock %}{% block content %} <section> <div class="wp butler-detail-box butler-diaryd-box clearfix"> <div class="fl list"> <div class="butler_detail_list clearfix"> <div class="brief"> <dl class="des"> <dt> <div class="picpic"> <img width="100" height="100" src="{{ MEDIA_URL }}{{ teacher.image }}"/> </div> <div class="btn"> <span class="fl btn1 collectionbtn" id="jsLeftBtn"> {% if has_teacher_faved %}已收藏{% else %}收藏{% endif %} </span> <span class="fr btn2 shareBtn bdsharebuttonbox" data-text="授课教师-李老师-慕学网" data-desc="我在#慕课网#发现了" data-comment="李老师,工作年限:5年;学历:本科;所在公司:阿里巴巴 ;经典案例:django入门和深入;flask入门" data-url="/diary/hk_detail/10/"> <span class="fl">分享</span> <a href="#" class="bds_more" data-cmd="more"></a> </span> </div> </dt> <dd> <a href="/diary/hk_detail/10/"> <h1>{{ teacher.name }}<span class="key picbig">金牌讲师</span></h1> </a> <ul class="cont"> <li>工作年限:<span>{{ teacher.work_years }}</span></li> <li>就职公司:<span>{{ teacher.work_company }}</span></li> <li>工作职位:<span>{{ teacher.work_position }} </span></li> <li>教学特点:<span>{{ teacher.points }}</span></li> </ul> </dd> </dl> </div> </div> <div class="butler_detail_cont clearfix"> <div class="left layout"> <div class="head"> <ul class="tab_header"> <li class="active"><a href="">全部课程</a> </li> </ul> </div> <div class="companycenter"> <div class="group_list brief"> {% for teacher_course in all_courses %} <div class="module1_5 box"> <a href="{% url 'course:course_detail' teacher_course.id %}"> <img width="214" height="190" class="scrollLoading" src="{{ MEDIA_URL }}{{ teacher_course.image }}"/> </a> <div class="des"> <a href="{% url 'course:course_detail' teacher_course.id %}"><h2>{{ teacher_course.name }}</h2></a> <span class="fl">时长:<i class="key">{{ teacher_course.learn_times }}</i></span> <span class="fr">学习人数:{{ teacher_course.learn_times }}</span> </div> <div class="bottom"> <span class="fl">{{ teacher_course.course_org.name }}</span> <span class="star fr notlogin" data-favid="15">{{ teacher_course.course_org.fav_num }}</span> </div> </div> {% endfor %} </div> </div> <!--<div class="pageturn">--> <!--<ul class="pagelist">--> <!--<li class="active"><a href="?page=1">1</a></li>--> <!--</ul>--> <!--</div>--> </div> </div> </div> <div class="fr list"> <div class="butler_detail_list"> <div class="right butler-company-box"> <div class="head"> <h1>{{ teacher.org.name }}</h1> <p>知名高校,权威教学</p> </div> <div class="pic"> <a href="{% url 'org:org_home' teacher.org.id %}"> <img width="150" height="80" src="{{ MEDIA_URL }}{{ teacher.org.image }}"/> </a> </div> <a href="{% url 'org:org_home' teacher.org.id %}"> <h2 class="center">{{ teacher.org.name }}</h2> </a> <p class="center tell">地址:{{ teacher.org.address }}</p> <a class="btn" id="jsRightBtn">{% if has_org_faved %}已收藏{% else %}收藏{% endif %}</a> </div> </div> <div class="butler_detail_cont"> <div class="butler_list_box"> <div class="right layout"> <div class="head">讲师排行榜</div> {% for hot_teacher in sorted_teacher %} <dl class="des"> <span class="num fl">1</span> <a href="{% url 'org:teacher_detail' hot_teacher.id %}"> <dt> <img width="50" height="50" class="scrollLoading" src="{{ MEDIA_URL }}{{ hot_teacher.image }}"/> </dt> </a> <dd> <a href="{% url 'org:teacher_detail' hot_teacher.id %}"> <h1 title="bobby">{{ hot_teacher.name }}</h1> </a> <p>工作年限:<span>{{ hot_teacher.work_years }}</span></p> </dd> </dl> {% endfor %} </div> </div> </div> </div> </div> </section>{% endblock %}
其中base.html中已经写好了公共部分,根据具体情况填写其他特殊部分,{% block content %}部分通过分析源页面中该页面独有部分拷贝过来。from .views import TeacherListView, TeacherDetailView, AddFavViewurlpatterns = [ ... # 机构收藏 url(r'^add_fav/$', AddFavView.as_view(), name='add_fav'), ... # 讲师列表页 url(r'^teacher/list/$', TeacherListView.as_view(), name='teacher_list'), # 讲师详情页 url(r'^teacher/detail/(?P<teacher_id>/d+)/$', TeacherDetailView.as_view(), name='teacher_detail'),]
3.添加并完成views.py的功能...class AddFavView(View): """ 用户收藏,用户取消收藏 """ def post(self, request): fav_id = request.POST.get('fav_id', 0) # 防止后边int(fav_id)时出错 fav_type = request.POST.get('fav_type', 0) # 防止int(fav_type)出错 if not request.user.is_authenticated(): # 判断用户登录状态 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exist_record = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exist_record: # 如果记录已经存在,表示用户取消收藏 exist_record.delete() return HttpResponse('{"status":"fail", "msg":"已取消收藏"}', content_type='application/json') else: user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')class TeacherListView(View): """ 课程讲师列表页 """ def get(self, request): all_teachers = Teacher.objects.all() sort = request.GET.get('sort', "") if sort: if sort == "hot": all_teachers = all_teachers.order_by("-click_num") sorted_teacher = Teacher.objects.all().order_by("-click_num")[:3] # 对讲师进行分页 try: page = request.GET.get('page', 1) except PageNotAnInteger: page = 1 p = Paginator(all_teachers, 1, request=request) teachers = p.page(page) return render(request, "teachers-list.html", { "all_teachers":teachers, "sorted_teacher":sorted_teacher, "sort":sort, })class TeacherDetailView(View): def get(self, request, teacher_id): teacher = Teacher.objects.get(id=int(teacher_id)) all_courses = Course.objects.filter(teacher=teacher) has_teacher_faved = False if UserFavorite.objects.filter(user=request.user, fav_type=3, fav_id=teacher.id): has_teacher_faved = True has_org_faved = False if UserFavorite.objects.filter(user=request.user, fav_type=2, fav_id=teacher.org.id): has_org_faved = True # 讲师排行 sorted_teacher = Teacher.objects.all().order_by("-click_num")[:3] return render(request, "teacher-detail.html", { "teacher":teacher, "all_courses":all_courses, "sorted_teacher":sorted_teacher, "has_teacher_faved":has_teacher_faved, "has_org_faved":has_org_faved, })
关键词:标准,使用