路飞学城1之课程与课程详细

 

 

前端vue

路飞学城1之课程与课程详细_第1张图片

 

 

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
main.js

 





App.vue

 

import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import course from './views/course'
import courseDetail from './views/courseDetail'
import login from './views/Login'



Vue.use(Router)

export default new Router({
    mode: 'history',
    routes: [
        {
            path: '/',
            name: 'home',
            component: Home
        },

        {
            path: '/course/',
            name: 'course',
            component: course
        },
         {
            path: '/courseDetail/:id',
            name: 'courseDetail',
            component: courseDetail
        },
         {
            path: '/login/',
            name: 'login',
            component: login
        }




    ]
})
router.js

 





course.vue

 





courseDetail.vue

跨域

from django.utils.deprecation import MiddlewareMixin

class CORSMiddleware(MiddlewareMixin):

    def process_response(self,request,response):
        # 添加响应头

        # 允许你的域名来获取我的数据
        response['Access-Control-Allow-Origin'] = "*"

        # 允许你携带Content-Type请求头
        # response['Access-Control-Allow-Headers'] = "Content-Type"

        # 允许你发送DELETE,PUT
        # response['Access-Control-Allow-Methods'] = "DELETE,PUT,GET"

        return response

 settings.py:

   MIDDLEWARE = [ 'api.util.mycor.CORSMiddleware', ] 

 

后端Django

from django.conf.urls import url,include
from django.contrib import admin
from api import views
from rest_framework import routers
router=routers.DefaultRouter()
router.register('course',views.courseView)
router.register('courseDetail',views.courseDetailView)
router.register('ChapterView',views.ChapterView)

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^login/', views.login.as_view()),
    url(r'^', include(router.urls)),

]
urls.py

 

from django.shortcuts import render
from rest_framework.views import APIView
from api.models import *
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from api.util.Myserializers import *
from rest_framework.response import Response
from rest_framework import exceptions
from django.http import JsonResponse

class courseView(ModelViewSet):
   
    queryset = Course.objects.all()
    serializer_class = courseViewSerializer

class courseDetailView(ModelViewSet):
    queryset = CourseDetail.objects.all()
    serializer_class = courseDetailViewSerializer


class ChapterView(ModelViewSet):
    queryset = Chapter.objects.all()
    serializer_class = ChapterViewSerializer
views.py
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from api.models import *

from api.models import *

class courseViewSerializer(serializers.ModelSerializer):
    class Meta:
        model=Course
        fields='__all__'

class courseDetailViewSerializer(serializers.ModelSerializer):
    course=serializers.CharField(source='course.title')
    recommend_courses=serializers.SerializerMethodField()
    class Meta:
        model=CourseDetail
        fields='__all__'
    def get_recommend_courses(self,obj):
        temp=[]
        for course in obj.recommend_courses.all():
            temp.append({'id':course.id,'title':course.title})

        return temp

class ChapterViewSerializer(serializers.ModelSerializer):
    class Meta:
        model=Chapter
        fields='__all__'
Myserializers.py

 

转载于:https://www.cnblogs.com/wanghuaqiang/p/9267181.html

你可能感兴趣的:(python,后端,javascript)