分类: Python/Ruby
2021-06-11 16:58:56
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) # 把前端传过来的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})