人人都能看懂的Django REST framework

Posted Gendan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了人人都能看懂的Django REST framework相关的知识,希望对你有一定的参考价值。

from django.http import JsonResponse
from django.shortcuts import render

Create your views here.

from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from app03.serializers import ArticleSerializer, CategorySerializer
from app03.models import *
@csrf_exempt
def article_list(request):

if request.method == \'GET\':
    arts = Article.objects.all()
    ser = ArticleSerializer(instance=arts, many=True, context={\'request\': request})
    return JsonResponse({\'status\': 200, \'data\': ser.data})
elif request.method == \'POST\':
    data = JSONParser().parse(request)  # 把前端传过来的json数据转为python里面的数据类型
    ser = ArticleSerializer(data=data, context={\'request\': request})
    if ser.is_valid():
        ser.save()
        return JsonResponse({\'status\': 201, \'data\': ser.data})
    return JsonResponse({\'status\': 400, \'data\': ser.errors})

@csrf_exempt
def article_detail(request, pk):

try:
    art = Article.objects.get(pk=pk)
except Article.DoesNotExist as e:
    return JsonResponse({\'msg\': "未获取到pk值", \'status\': 404})
if request.method == \'GET\':
    ser = ArticleSerializer(instance=art, context={\'request\': request})
    return JsonResponse({\'status\': 200, \'data\': ser.data})
elif request.method == \'PUT\':
    data = JSONParser().parse(request)
    ser = ArticleSerializer(instance=art, data=data, context={\'request\': request})
    if ser.is_valid():
        ser.save()
        return JsonResponse({\'status\': 201, \'data\': ser.data})
    return JsonResponse({\'status\': 400, \'data\': ser.errors})
elif request.method == \'PATCH\':
    data = JSONParser().parse(request)
    ser = ArticleSerializer(instance=art, data=data, partial=True, context={\'request\': request})
    if ser.is_valid():
        ser.save()
        return JsonResponse({\'status\': 201, \'data\': ser.data})
    return JsonResponse({\'status\': 400, \'data\': ser.errors})
elif request.method == \'DELETE\':
    art.delete()
    return JsonResponse({\'msg\': \'删除成功\', \'status\': 204})

@csrf_exempt
def category_list(request):

if request.method == \'GET\':
    cats = Category.objects.all()
    ser = CategorySerializer(instance=cats, many=True, context={\'request\': request})
    return JsonResponse({\'status\': 200, \'data\': ser.data})
elif request.method == \'POST\':
    data = JSONParser().parse(request)  # [期货](https://www.gendan5.com/futures.html)把前端传过来的json数据转为python里面的数据类型
    ser = CategorySerializer(data=data, context={\'request\': request})
    if ser.is_valid():
        ser.save()
        return JsonResponse({\'status\': 201, \'data\': ser.data})
    return JsonResponse({\'status\': 400, \'data\': ser.errors})

@csrf_exempt
def category_detail(request, pk):

try:
    art = Category.objects.get(pk=pk)
except Category.DoesNotExist as e:
    return JsonResponse({\'msg\': "未获取到pk值", \'status\': 404})
if request.method == \'GET\':
    ser = CategorySerializer(instance=art, context={\'request\': request})
    return JsonResponse({\'status\': 200, \'data\': ser.data})
elif request.method == \'PUT\':
    data = JSONParser().parse(request)
    ser = CategorySerializer(instance=art, data=data, context={\'request\': request})
    if ser.is_valid():
        ser.save()
        return JsonResponse({\'status\': 201, \'data\': ser.data})
    return JsonResponse({\'status\': 400, \'data\': ser.errors})
elif request.method == \'PATCH\':
    data = JSONParser().parse(request)
    ser = CategorySerializer(instance=art, data=data, partial=True, context={\'request\': request})
    if ser.is_valid():
        ser.save()
        return JsonResponse({\'status\': 201, \'data\': ser.data})
    return JsonResponse({\'status\': 400, \'data\': ser.errors})
elif request.method == \'DELETE\':
    art.delete()
    return JsonResponse({\'msg\': \'删除成功\', \'status\': 204})

以上是关于人人都能看懂的Django REST framework的主要内容,如果未能解决你的问题,请参考以下文章

人人都能看懂的Django REST framework

人人都能看懂的Spring源码解析,Spring如何解决循环依赖

人人都能看懂的LSTM

收藏 | 人人都能看懂的 LSTM

人人都能看懂的LSTM

人人都能看懂的云计算知识科普 !