Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3648397
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: 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})

阅读(885) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~