课程展示
#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 } ]
遇见的问题
跨域
需要在settings.py中注册中间件
#utils/middlewares.py
from django.utils.deprecation import MiddlewareMixin class CoreMiddleWares(MiddlewareMixin): def process_response(self,request,response): #由于是简单请求 post response["Access-Control-Allow-Origin"]="*" return response