zoukankan      html  css  js  c++  java
  • day 73作业

    作业:
    1.、简单过过请求模块的源码流程,建立一个视图类,完成一项渲染模块与解析模块的全局,局部配置
    2.、在自己项目中自定义一下异常处理函数,并配置给项目

    全局配置:
        
    REST_FRAMEWORK = {
            # 解析模块
        'DEFAULT_PARSER_CLASSES' : {
            'rest_framework.parsers.JSONParser', 
            'rest_framework.parsers.FormParse', 
            'rest_framework.parsers.JSONParser',  
        },
            #渲染模块
        'DEFAULT_RENDERER_CLASSES': {
            'rest_framework.renderers.JSONRenderer',    
            'rest_framework.renderers.BrowsableAPIRenderer',
        },
            # 自定义配置异常模块
         'EXCEPTION_HANDLER': 'api.exception.exception_handler', 
    }
    
    
    视图类局部配置:
    
    from rest_framework.parsers import JSONParser,FormParser,MultiPartParser
    from rest_framework.renderers import JSONRenderer
    from rest_framework.renderers import BrowsableAPIRenderer
    from rest_framework.views import APIView
    from rest_framework.response import Response
    
    class Student(APIView):
        parser_classes = [JSONParseer]   #局部解析配置
        renderer_classes = [JSONRenderer] #局部渲染配置
        
        def get(self,request,*args,**kwargs):
            return Response('drf get ok')
    
        def post(self,request,*args,**kwargs):
            return Response('drf post ok')
    
        
    from rest_framework.views import exception_handler as drf_exception_handler
    from rest_framework.response import Response
    from rest_framework import status  
    
    def exception_handler(exc, context):     
        response = drf_exception_handler(exc, context)
        detail = '%s-%s-%s' %(context.get('view'),context.get('request'),exc)
        if not response:  
            response = Response({'detail':detail},, status=status.HTTP_500_INTERNAL_SERVER_ERROR, exception=True)
        else:
            response.data = {'detail':detail}
        return response
             
    urls.py:
        from django.conf.urls import url, include
        from django.contrib import admin
    
        urlpatterns = [
            url(r'^admin/', admin.site.urls),
            url(r'^api/', include('api.urls')),
        ]
    
    api.urls.py:
        
        from django.conf.urls import url
        from . import  views
        urlpatterns = {
            url(r'^student/$',views.Student.as_view()),
        }
    
  • 相关阅读:
    在Windows QT下使用ZeroMQ
    libpng warning: iCCP: known incorrect sRGB profile告警处理
    qt 程序发布
    Qtcreator 之中文目录
    windows下kafka配置入门 示例
    CentOS-7安装Mysql集群
    zookeeper 集群安装(单点与分布式成功安装)摘录
    Linux下安装Redis2.6.17
    Hadoop集群Hadoop安装配置
    lvs/dr+keepalived应用测试实施文档
  • 原文地址:https://www.cnblogs.com/colacheng0930/p/12120676.html
Copyright © 2011-2022 走看看