Python打造線上教學平臺——學習週報5
阿新 • • 發佈:2018-12-30
業務邏輯開發
—————— 學習週報 2018.12.9
一 、本週計劃:
- 完成第7章—— 課程機構功能實現(7-9------- 7-12)
- 完成第8章——課程功能實現(8-1)
二 、完成情況:
- 1.機構詳情展示
- 2.課程機構收藏 或者 使用者取消收藏功能
- 3.課程列表
- 功能1:課程排序
- 功能2:課程自動分頁
- 功能3: 熱門課程推薦
- 功能4:課程搜尋功能
三、 展示:
錯誤1
原因:因為在organiztion/adminx中,這個地方多了一個空格,導致與model中定義的對應不上,(所以寫程式碼時要認真!!!)
1、機構詳情展示
- 後臺資料庫增加講師,增加課程(加外來鍵)
- 4個HTML檔案匯入template,重寫HTML檔案
- 編寫organization/urls.py和organization/views.py
# organization/urls.py的部分程式碼 from django.conf.urls import url, include from .views import OrgView, AddUserAskView, OrgHomeView, OrgCourseView 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+)/$', OrgHomeView.as_view(), name="org_home"), url(r'^course/(?P<org_id>\d+)/$', OrgCourseView.as_view(), name="org_course"), url(r'^desc/(?P<org_id>\d+)/$', OrgDescView.as_view(), name="org_desc"), url(r'^org_teacher/(?P<org_id>\d+)/$', OrgTeacherView.as_view(), name="org_teacher"), ]
# organization/views.py的部分程式碼 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, 'course_org':course_org, }) class OrgDescView(View): """ 機構介紹頁 """ def get(self, request, org_id): current_page = "desc" course_org = CourseOrg.objects.get(id=int(org_id)) has_fav = False if request.user.is_authenticated(): if UserFavorite.objects.filter(user=request.user, fav_id=course_org.id, fav_type=2): has_fav = True return render(request, 'org-detail-desc.html', { 'course_org':course_org, 'current_page':current_page, 'has_fav':has_fav }) class OrgTeacherView(View): """ 機構教師頁 """ def get(self, request, org_id): current_page = "teacher" course_org = CourseOrg.objects.get(id=int(org_id)) has_fav = False if request.user.is_authenticated(): if UserFavorite.objects.filter(user=request.user, fav_id=course_org.id, fav_type=2): has_fav = True all_teachers = course_org.teacher_set.all() return render(request, 'org-detail-teachers.html', { 'all_teachers':all_teachers, 'course_org':course_org, 'current_page':current_page, 'has_fav':has_fav }) class OrgCourseView(View): """ 機構課程列表頁 """ def get(self, request, org_id): 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, })
- 機構首頁介面:
- 機構課程介面:
- 機構介紹介面:
- 機構講師介面:
2.課程機構收藏 或者 使用者取消收藏功能
- 編寫organization/urls.py和organization/views.py
在view中方法要注意邏輯,先要判斷是否登入,才允許進行收藏!!!!
# organization/urls.py的程式碼
# 機構收藏
url(r'^add_fav/$', AddFavView.as_view(), name="add_fav"),
# organization/views.py的程式碼
class AddFavView(View):
"""
使用者收藏,使用者取消收藏
"""
def post(self, request):
fav_id = request.POST.get('fav_id', 0)
fav_type = request.POST.get('fav_type', 0)
if not request.user.is_authenticated():
#判斷使用者登入狀態
return HttpResponse('{"status":"fail", "msg":"使用者未登入"}', content_type='application/json')
exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type))
if exist_records:
#如果記錄已經存在, 則表示使用者取消收藏
exist_records.delete()
if int(fav_type) == 1:
course = Course.objects.get(id=int(fav_id))
course.fav_nums -= 1
if course.fav_nums < 0:
course.fav_nums = 0
course.save()
elif int(fav_type) == 2:
course_org = CourseOrg.objects.get(id=int(fav_id))
course_org.fav_nums -= 1
if course_org.fav_nums < 0:
course_org.fav_nums = 0
course_org.save()
elif int(fav_type) == 3:
teacher = Teacher.objects.get(id=int(fav_id))
teacher.fav_nums -= 1
if teacher.fav_nums < 0:
teacher.fav_nums = 0
teacher.save()
return HttpResponse('{"status":"success", "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()
if int(fav_type) == 1:
course = Course.objects.get(id=int(fav_id))
course.fav_nums += 1
course.save()
elif int(fav_type) == 2:
course_org = CourseOrg.objects.get(id=int(fav_id))
course_org.fav_nums += 1
course_org.save()
elif int(fav_type) == 3:
teacher = Teacher.objects.get(id=int(fav_id))
teacher.fav_nums += 1
teacher.save()
return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json')
else:
return HttpResponse('{"status":"fail", "msg":"收藏出錯"}', content_type='application/json')
-
點選收藏,再點選取消收藏:
再檢查資料庫:——(記錄已經存在!!!!)
-
更改頁面頂部,只要登入,每一個頁面都顯示登入,(靜態頁面變為動態頁面):
<div class="top">
<div class="wp">
<div class="fl"><p>服務電話:<b>18889874563</b></p></div>
<!--登入後跳轉-->
{% if request.user.is_authenticated %}
<div class="personal">
<dl class="user fr">
<dd>{{ user.username }}<img class="down fr" src="/static/images/top_down.png"/></dd>
<dt><img width="20" height="20" src="/static/media/image/2016/12/default_big_14.png"/></dt>
</dl>
<div class="userdetail">
<dl>
<dt><img width="80" height="80" src="/static/media/image/2016/12/default_big_14.png"/></dt>
<dd>
<h2>{{ request.user.nick_name }}</h2>
<p>{{ request.user.username }}</p>
</dd>
</dl>
<div class="btn">
<a class="personcenter fl" href="usercenter-info.html">進入個人中心</a>
<a class="fr" href="/logout/">退出</a>
</div>
</div>
</div>
{% else %}
<a style="color:white" class="fr registerbtn" href="{% url 'register' %}">註冊</a>
<a style="color:white" class="fr loginbtn" href="{% url 'login' %}">登入</a>
{% endif %}
</div>
</div>
3.課程列表
- 匯入course_list.html,更改;設定course的urls.py和views.py
- 訪問頁面“公開課”後,沒有動態資料,所以在後臺xadmin中新增資料:
- 功能1:實現自動分頁:
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
# 對課程進行分頁
try:
page = request.GET.get('page', 1)
except PageNotAnInteger:
page = 1
p = Paginator(all_courses, 3, request=request) # 3個課程一個頁面
courses = p.page(page)
(12個課程,3個一頁,自動分為4頁)
- 功能2: 實現排序——預設排序按最新的排序
# 課程排序
sort = request.GET.get('sort', "")
if sort:
if sort == "students":
all_courses = all_courses.order_by("-students")
elif sort == "hot":
all_courses = all_courses.order_by("-click_nums")
- 功能3: 最熱門課程推薦——根據點選數決定
- course_list.html的程式碼:
<div class="right layout">
<div class="head">熱門課程推薦</div>
<div class="group_recommend">
{% for hot_course in hot_courses %}
<dl>
<dt>
<a target="_blank" href="{{ ecmall_ad.url }}">
<img width="240" height="220" class="scrollLoading" src="{{ MEDIA_URL }}{{ hot_course.image }}"/>
</a>
</dt>
<dd>
<a target="_blank" href="{{ ecmall_ad.url }}"><h2> {{ hot_course.name }}</h2></a>
<span class="fl">難度:<i class="key">{{ hot_course.get_degree_display }}</i></span>
</dd>
</dl>
{% endfor %}
</div>
</div>
- views.py的class CourseListView(View)程式碼:
hot_courses = Course.objects.all().order_by("-click_nums")[:3]
- 功能4:課程搜尋功能
# 課程搜尋
search_keywords = request.GET.get('keywords', "")
if search_keywords:
all_courses = all_courses.filter(
Q(name__icontains=search_keywords) | Q(desc__icontains=search_keywords) | Q(
detail__icontains=search_keywords))
下週任務:
- 完成第8章——課程功能實現(8-2 ~ 8-8)
- 1.課程詳情頁
- 2.課程章節資訊
- 3.課程評論功能
- 4.相關課程推薦
- 5.視訊播放頁面
- 完成第9章——課程講師功能實現(9-1 ~ 9-3)
- .講師列表頁
- 講師詳情頁