1. 程式人生 > >AI學習吧-課程展示

AI學習吧-課程展示

 

#urls.py
from django.conf.urls import url,include
from django.contrib import admin
from app01.view.course import Courseview,CourseDetailview

from rest_framework import routers
router=routers.DefaultRouter()
router.register("courses",Courseview)  #課程類
router.register("coursedetail"
,CourseDetailview) #課程詳情類 urlpatterns = [ url(r'^admin/', admin.site.urls), url("^", include(router.urls)), #使用url註冊器製作 ]

 

#app01/view/course.py  #寫一個資料夾view,刪除views.py,專門存放檢視函式
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from
..models import * from app01.utils.serializer import * class Courseview(ModelViewSet): queryset = Course.objects.all() serializer_class = CourseSerializers

 

#app01/view/coursedetail.py

from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from ..models import * from app01.utils.serializer import * class CourseDetailview(ModelViewSet): queryset = CourseDetail.objects.all() serializer_class = CourseDetailSerlizers

 

#app01/utils/serializer.py   #專門存放序列化元件

from rest_framework import serializers
from ..models import *

class CourseSerializers(serializers.ModelSerializer):

    level=serializers.SerializerMethodField()   #前端需要關聯表中的欄位,而不是id值
    def get_level(self,obj):
        return obj.get_level_display()


    class Meta:
        model=Course
        fields="__all__"



class CourseDetailSerlizers(serializers.ModelSerializer):

    course_name=serializers.CharField(source="course.name")
    price_policy_list=serializers.SerializerMethodField()
    recommend_courses=serializers.SerializerMethodField()
    teachers=serializers.SerializerMethodField()


    def get_recommend_courses(self,obj):  #將推薦課程,裡的具體內容傳給前端頁面,而不是僅傳id值

        temp=[]
        for course in obj.recommend_courses.all():
            temp.append({
                "pk":course.pk,
                "name":course.name
            })

        return temp

    #比如寫個訪問老師資訊時:(參考本段紅色程式碼,還有postman紅色程式碼即可),如果不這麼寫,就會返回老師ID值。
    def get_teachers(self,obj):

        temp=[]
        for teacher in obj.teachers.all():
            temp.append({
                "pk":teacher.pk,
                "name":teacher.name
            })

        return temp

    def get_price_policy_list(self,obj):
        temp=[]
        for price_obj in obj.course.price_policy.all():
            temp.append({
                "pk":price_obj.pk,
                "price":price_obj.price,
                "valid_period":price_obj.valid_period,
                "valid_period_text":price_obj.get_valid_period_display(),
            })

        return temp

    class Meta:
        model=CourseDetail
        fields="__all__"

 

訪問http://127.0.0.1:8000/coursedetail/時,效果

[
    {
        "id": 1,
        "course_name": "python全棧",
        "price_policy_list": [
            {
                "pk": 1,
                "price": 100,
                "valid_period": 7,
                "valid_period_text": "1周"
            },
            {
                "pk": 2,
                "price": 200,
                "valid_period": 14,
                "valid_period_text": "2周"
            },
            {
                "pk": 3,
                "price": 300,
                "valid_period": 180,
                "valid_period_text": "6個月"
            }
        ],
        "recommend_courses": [
            {
                "pk": 1,
                "name": "python全棧"
            }
        ],
        "teachers": [
            {
                "pk": 1,
                "name": "alex"
            }
        ],
        "hours": 200,
        "course_slogan": "python,讓世界上的程式設計師不再禿頂",
        "course": 1
    },
    {
        "id": 2,
        "course_name": "Linux雲端計算運維",
        "price_policy_list": [
            {
                "pk": 4,
                "price": 0,
                "valid_period": 7,
                "valid_period_text": "1周"
            },
            {
                "pk": 5,
                "price": 100,
                "valid_period": 14,
                "valid_period_text": "2周"
            },
            {
                "pk": 6,
                "price": 300,
                "valid_period": 180,
                "valid_period_text": "6個月"
            }
        ],
        "recommend_courses": [
            {
                "pk": 1,
                "name": "python全棧"
            }
        ],
        "teachers": [
            {
                "pk": 2,
                "name": "egon"
            }
        ],
        "hours": 400,
        "course_slogan": "linux,每個程式設計師必會的東東",
        "course": 2
    },
    {
        "id": 3,
        "course_name": "演算法入門",
        "price_policy_list": [
            {
                "pk": 7,
                "price": 500,
                "valid_period": 180,
                "valid_period_text": "6個月"
            }
        ],
        "recommend_courses": [
            {
                "pk": 1,
                "name": "python全棧"
            }
        ],
        "teachers": [
            {
                "pk": 4,
                "name": "Doctor Zhao"
            }
        ],
        "hours": 30,
        "course_slogan": "演算法是這個世界的磚瓦",
        "course": 3
    }
]