您现在的位置是: 网站首页 >Django >Django2.0.8+xadmin2实现在线学习网站 Django
【Django在线教育平台】15.授课讲师列表和详情功能
admin2019年6月4日 14:13 【Django | Html | JQuery 】 1124人已围观
Django2.0.8+xadmin2实现在线学习网站简介 Django2.0.8+xadmin2实现在线学习网站,课程、讲师、机构、用户收藏功能。GitHub地址:https://github.com/xyliurui/OnlineLearningPlatform ;Django版本:2.0.8
# 授课教师相关功能 ## 创建讲师相关url 在organization应用下创建urls2.py用于放置讲师有关的url ```python from django.urls import path, re_path app_name = 'teacher' urlpatterns = [ # 讲师相关url ] ``` 修改主urls.py,添加讲师urls ```python from django.contrib import admin from django.urls import path, re_path from django.conf.urls.static import static from django.conf import settings from django.views.generic import TemplateView from django.urls import include import xadmin from users.views import user_login, LoginView, RegisterView, ActiveUserView, ForgetPwdView, RestpwdView, ModifypwdView from organization.views import OrgListView urlpatterns = [ path('admin/', admin.site.urls), path('xadmin/', xadmin.site.urls), path('', TemplateView.as_view(template_name='index.html'), name='index'), # path('login/', TemplateView.as_view(template_name='login.html'), name='login'), # path('login/', user_login, name='login'), path('login/', LoginView.as_view(), name='login'), # 基于类方法实现登录,这里是调用它的方法 path('register/', RegisterView.as_view(), name='register'), re_path('register/active/(?P<active_code>.*)/', ActiveUserView.as_view(), name='user_active'), # 激活 path('captcha/', include('captcha.urls')), path('forgetpwd/', ForgetPwdView.as_view(), name='forgetpwd'), # 忘记密码 re_path('forgetpwd/reset/(?P<active_code>.*)/', RestpwdView.as_view(), name='resetpwd'), # 密码重置验证 path('modify_pwd/', ModifypwdView.as_view(), name="modify_pwd"), # 密码修改 # path('org/list/', OrgListView.as_view(), name="org_list"), # 机构列表 # 课程机构url配置 path('org/', include('organization.urls', namespace='org')), # 课程相关功能url path('course/', include('courses.urls', namespace='course')), # 讲师 path('teacher/', include('organization.urls2', namespace='teacher')), ] ``` ## 讲师列表功能 ### 讲师列表视图 organization.views.py创建视图 ```python # 讲师列表 class TeacherListView(View): def get(self, request): all_teacher = Teacher.objects.all().order_by('-click_nums') teacher_nums = all_teacher.count() sort = request.GET.get('sort', '') if sort: if sort == 'fav': all_teacher = all_teacher.order_by('-fav_nums') try: page = request.GET.get('page', 1) except PageNotAnInteger: page = 1 # 这里每页显示5个 p = Paginator(all_teacher, 5, request=request) all_teacher_page = p.page(page) # 排行榜讲师 rank_teacher = Teacher.objects.all().order_by("-fav_nums", "click_nums")[:5] # 标记当前页,用于页面选中active current_access_url = 'teacher' return render(request, 'teacher-list.html', locals()) ``` ### 讲师列表url organization.urls.py ```python from django.urls import path, re_path from .views import TeacherListView app_name = 'teacher' urlpatterns = [ # 讲师相关url path('list/', TeacherListView.as_view(), name='teacher_list'), ] ``` ### 讲师列表模板teacher-list.html ```html <h2>授课教师 <small>(共{{ teacher_nums }}名,本页显示{{ all_teacher_page.object_list.count }}名)</small> </h2> <ul> <!-- Short List --> <li> <p><a href="{% url 'teacher:teacher_list' %}" {% ifequal sort '' %} style="font-weight: bold" {% endifequal %}> 点击数(默认) </a></p> </li> <li> <p><a href="{% url 'teacher:teacher_list' %}?sort=fav" {% ifequal sort 'fav' %} style="color: #BEBEBE" {% endifequal %}> 收藏人气 ↓</a></p> </li> </ul> {% for teacher in all_teacher_page.object_list %} {% endfor %} <ul class="pagination"> {% if all_teacher_page.has_previous %} <li><a href="?{{ all_teacher_page.previous_page_number.querystring }}" aria-label="Previous"> <i class="fa fa-angle-left"></i> </a></li> {% endif %} {% for page in all_teacher_page.pages %} {% if page %} {% ifequal page all_teacher_page.number %} <li><a class="active" href="?{{ page.querystring }}">{{ page }}</a></li> {% else %} <li><a href="?{{ page.querystring }}">{{ page }}</a></li> {% endifequal %} {% else %} <li><a href="">...</a></li> {% endif %} {% endfor %} {% if all_teacher_page.has_next %} <li><a href="?{{ all_teacher_page.next_page_number.querystring }}" aria-label="Next"> <i class="fa fa-angle-right"></i> </a></li> {% endif %} </ul> ``` ![BLOG_20190604_141436_21](/media/blog/images/2019/06/BLOG_20190604_141436_21.png "博客图集BLOG_20190604_141436_21.png") ## 讲师详情功能 ### 讲师详情视图 ```python # 讲师详情 class TeacherDetailView(View): def get(self, request, teacher_id): teacher = Teacher.objects.get(id=teacher_id) # 增加讲师的访问量 teacher.click_nums += 1 teacher.save(update_fields=['click_nums']) # 排行榜讲师 rank_teacher = Teacher.objects.all().order_by("-fav_nums", "click_nums")[:5] # 讲师和机构是否已收藏 has_fav_teacher = False has_fav_org = False # 必须是用户已登录我们才需要判断。 if request.user.is_authenticated: if UserFavorite.objects.filter(user=request.user, fav_id=teacher.id, fav_type=3): # 该讲师已收藏 has_fav_teacher = True if UserFavorite.objects.filter(user=request.user, fav_id=teacher.org.id, fav_type=2): # 该机构已收藏 has_fav_org = True # 标记当前页,用于页面选中active current_access_url = 'teacher' return render(request, 'teacher-detail.html', locals()) ``` ### 讲师详情url organization.urls2.py ```python from django.urls import path, re_path from .views import TeacherListView, TeacherDetailView app_name = 'teacher' urlpatterns = [ # 讲师相关url path('list/', TeacherListView.as_view(), name='teacher_list'), re_path('id/(?P<teacher_id>\d+)/detail/', TeacherDetailView.as_view(), name='teacher_detail'), ] ``` ### 讲师详情模板与收藏 ```html <ol class="breadcrumb"> <li><a href="#">主页</a></li> <li><a href="{% url 'teacher:teacher_list' %}">授课教师</a></li> <li class="active">{{ teacher.name }}</li> </ol> <ul class="cmp-list"> {# 收藏与取消收藏课程#} <i class="fa fa-heart" style="color: #FF8040"></i> {% if has_fav_teacher %} <a href="javascript:void(0)" id="add_fav_teacher_button" style="color: #FFA042"> 取消收藏</a> {% else %} <a href="javascript:void(0)" id="add_fav_teacher_button" style="color: #02DF82"> 添加收藏</a> {% endif %} </ul> {% for course in teacher.courses.all %} {% empty %} 暂无 {% endfor %} <p> {# 机构收藏与取消机构收藏 #} {% if has_fav_org %} <button id="add_fav_org_button" class="btn-round btn-light" style="background: #FFA042">取消收藏</button> {% else %} <button id="add_fav_org_button" class="btn-round btn-light" style="background: #02DF82">添加收藏</button> {% endif %} </p> {% block js %} <script type="text/javascript"> //添加或取消收藏 function add_fav(current_elem, fav_id, fav_type) { $.ajax({ cache: false, type: "POST", url: "{% 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.fav_status === 'fail') { if (data.fav_msg === '用户未登录') { window.location.href = "/login/?next={{ request.path }}"; } else { alert(data.fav_msg) } } else if (data.fav_status === 'success') { current_elem.text(data.fav_msg); // 修改按钮的颜色 if (data.fav_msg === '取消收藏') { if (fav_type === 2) { document.getElementById('add_fav_org_button').style.background = '#FFA042'; } else if (fav_type === 1) { document.getElementById('add_fav_course_button').style.color = '#FFA042'; } } else if (data.fav_msg === '添加收藏') { if (fav_type === 2) { document.getElementById('add_fav_org_button').style.background = '#02DF82'; } else if (fav_type === 1) { document.getElementById('add_fav_course_button').style.color = '#02DF82'; } } } }, }); } // 收藏机构点击 $('#add_fav_org_button').on('click', function () { add_fav($(this), {{ teacher.org.id }}, 2); }); // 收藏课程点击 $('#add_fav_teacher_button').on('click', function () { add_fav($(this), {{ teacher.id }}, 3); }); </script> {% endblock %} ``` ![BLOG_20190604_141426_62](/media/blog/images/2019/06/BLOG_20190604_141426_62.png "博客图集BLOG_20190604_141426_62.png")
很赞哦! (0)
相关文章
文章交流
- emoji
当前用户
未登录,点击 登录专题目录
- 【Django在线教育平台】01.创建Django虚拟环境和项目
- 【Django在线教育平台】02.创建该项目用到的数据库模型类
- 【Django在线教育平台】03.xadmin后台系统配置,将models注册到xadmin中
- 【Django在线教育平台】04.模板配置,完成用户登录相关页面和逻辑
- 【Django在线教育平台】05.用户注册功能实现
- 【Django在线教育平台】06.找回密码,实现忘记密码及重置密码功能
- 【Django在线教育平台】07.授课机构列表显示,分页、分类筛选、排序功能
- 【Django在线教育平台】08.学习咨询表单ajax提交
- 【Django在线教育平台】09.机构详情、机构课程详情页
- 【Django在线教育平台】10.机构讲师、机构介绍详情页
- 【Django在线教育平台】11.机构收藏功能实现
- 【Django在线教育平台】12.课程列表及详情展示
- 【Django在线教育平台】13.课程内容页,该课程评论功能
- 【Django在线教育平台】14.用户登陆后开始学习,播放课程视频
- 【Django在线教育平台】15.授课讲师列表和详情功能
- 【Django在线教育平台】16.导航选中状态,全局搜索功能实现
- 【Django在线教育平台】17.个人中心用户修改密码、头像
- 【Django在线教育平台】18.个人中心修改邮箱及其他信息
- 【Django在线教育平台】19.个人中心显示我的课程、收藏、消息页面
- 【Django在线教育平台】20.用户退出功能
- 【Django在线教育平台】21.其他功能完善
- 【Django在线教育平台】22.xadmin的其它常见功能