• DRF练习


    DRF 小练习

    """
    1、新建一个django项目,定义一个Car类,有name字符串属性,color整型选择属性(0:银色,1:黑色,2:红色,3:灰色),price全精度小数类型,image图片文件类型,brand字符串类型
    

    2、基于ModelSerializer类,完成Car资源的单查,群查,单增接口
    序列化:显示车名,车的颜色,车的价格,车的海报,车的品牌
    反序列化:必须提供车名,车的价格,车的品牌(车的匹配需要二次确认re_brand),车的颜色默认为银色(可以提供),车的海报不需要提供(默认为空)
    """

    settings.py

    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', 
    }

    views.py

    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] #局部渲染配置

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self,request,*args,**kwargs)</span>:</span>
        <span class="hljs-keyword">return</span> Response(<span class="hljs-string">'drf get ok'</span>)
    
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self,request,*args,**kwargs)</span>:</span>
        <span class="hljs-keyword">return</span> Response(<span class="hljs-string">'drf post ok'</span>)</code></pre>
    

    exception.py

    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

    urls.py:
        from django.conf.urls import url, include
        from django.contrib import admin
    
    urlpatterns = [
        url(<span class="hljs-string">r'^admin/'</span>, admin.site.urls),
        url(<span class="hljs-string">r'^api/'</span>, include(<span class="hljs-string">'api.urls'</span>)),
    ]
    

    api.urls.py:

    <span class="hljs-keyword">from</span> django.conf.urls <span class="hljs-keyword">import</span> url
    <span class="hljs-keyword">from</span> . <span class="hljs-keyword">import</span>  views
    urlpatterns = {
        url(<span class="hljs-string">r'^student/$'</span>,views.Student.as_view()),
    }</code></pre>
    
  • 相关阅读:
    Git学习-创建版本库
    使用Vim编辑器,如何退出
    设置既定目录的命令提示符
    字符数组和字符串
    一波杂乱的分享
    全国软件设计大赛C/C++语言练习
    HDU 1720、1062、2104、1064、2734、1170、1197、2629
    hdu 2000-2010 ACM
    HDU——算法练习1000 1089-1096
    爬虫学习笔记之为什么要设置超时时间,怎么设置(使用selenium)
  • 原文地址:https://www.cnblogs.com/jinhongquan/p/12120516.html
Copyright © 2020-2023  润新知