关于python:人人都能看懂的Django-REST-framework

46次阅读

共计 3122 个字符,预计需要花费 8 分钟才能阅读完成。

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})

正文完
 0