text: from django.shortcuts import render
  from django.http import HttpResponse
  from django.http import JsonResponse
  from django.views.decorators.csrf import csrf_exempt
  from rest_framework.parsers import JSONParser
  
  
  from snippets.models import Snippet
  from snippets.serializers import SnippetSerializer
  
  
  # Create your views here. 使用的django原生写法
  
  # 为了实现，允许没有CSRF令牌的客户端也可以请求接口
  @csrf_exempt
  def snippet_list(request):
      """
      列出所有snippet，或者创建一个snippet
      """
      if request.method == 'GET':
          snippets = Snippet.objects.all()
          serializer = SnippetSerializer(snippets, many=True)
          return JsonResponse(serializer.data, safe=False)
      if request.method == 'POST':
          data = JSONParser().parse(request)
          serializer = SnippetSerializer(data=data)
          if serializer.is_valid():
              serializer.save()
              return JsonResponse(serializer.data, status=201)
          return JsonResponse(serializer.errors, status=400)
  
  
  @csrf_exempt
  def snippet_detail(request, pk):
      """检索、更新、删除"""
      try:
          snippet = Snippet.objects.get(pk=pk)
      except Snippet.DoesNotExist:
          return HttpResponse(status=400)
  
      if request.method == 'GET':
          serializer = SnippetSerializer(snippet)
          return JsonResponse(serializer.data)
      elif request.method == 'PUT':
          data = JSONParser().parse(request)
          serializer = SnippetSerializer(snippet, data=data)
          if serializer.is_valid():
              serializer.save()
              return JsonResponse(serializer.data)
          return JsonResponse(serializer.errors, status=400)
      elif request.method == "DELETE":
          snippet.delete()
          return HttpResponse(status=204)
  
  
  
  