Writing regular Django views using our Serializer

Let's see how we can write some API views using our new Serializer class.For the moment we won't use any of REST framework's other features, we'll just write the views as regular Django views.

Edit the snippets/views.py file, and add the following.

  1. from django.http import HttpResponse, JsonResponse
  2. from django.views.decorators.csrf import csrf_exempt
  3. from rest_framework.parsers import JSONParser
  4. from snippets.models import Snippet
  5. from snippets.serializers import SnippetSerializer

The root of our API is going to be a view that supports listing all the existing snippets, or creating a new snippet.

  1. @csrf_exempt
  2. def snippet_list(request):
  3. """
  4. List all code snippets, or create a new snippet.
  5. """
  6. if request.method == 'GET':
  7. snippets = Snippet.objects.all()
  8. serializer = SnippetSerializer(snippets, many=True)
  9. return JsonResponse(serializer.data, safe=False)
  10. elif request.method == 'POST':
  11. data = JSONParser().parse(request)
  12. serializer = SnippetSerializer(data=data)
  13. if serializer.is_valid():
  14. serializer.save()
  15. return JsonResponse(serializer.data, status=201)
  16. return JsonResponse(serializer.errors, status=400)

Note that because we want to be able to POST to this view from clients that won't have a CSRF token we need to mark the view as csrf_exempt. This isn't something that you'd normally want to do, and REST framework views actually use more sensible behavior than this, but it'll do for our purposes right now.

We'll also need a view which corresponds to an individual snippet, and can be used to retrieve, update or delete the snippet.

  1. @csrf_exempt
  2. def snippet_detail(request, pk):
  3. """
  4. Retrieve, update or delete a code snippet.
  5. """
  6. try:
  7. snippet = Snippet.objects.get(pk=pk)
  8. except Snippet.DoesNotExist:
  9. return HttpResponse(status=404)
  10. if request.method == 'GET':
  11. serializer = SnippetSerializer(snippet)
  12. return JsonResponse(serializer.data)
  13. elif request.method == 'PUT':
  14. data = JSONParser().parse(request)
  15. serializer = SnippetSerializer(snippet, data=data)
  16. if serializer.is_valid():
  17. serializer.save()
  18. return JsonResponse(serializer.data)
  19. return JsonResponse(serializer.errors, status=400)
  20. elif request.method == 'DELETE':
  21. snippet.delete()
  22. return HttpResponse(status=204)

Finally we need to wire these views up. Create the snippets/urls.py file:

  1. from django.urls import path
  2. from snippets import views
  3. urlpatterns = [
  4. path('snippets/', views.snippet_list),
  5. path('snippets/<int:pk>/', views.snippet_detail),
  6. ]

We also need to wire up the root urlconf, in the tutorial/urls.py file, to include our snippet app's URLs.

  1. from django.urls import path, include
  2. urlpatterns = [
  3. path('', include('snippets.urls')),
  4. ]

It's worth noting that there are a couple of edge cases we're not dealing with properly at the moment. If we send malformed json, or if a request is made with a method that the view doesn't handle, then we'll end up with a 500 "server error" response. Still, this'll do for now.