In this Django REST Framework we are going to learn about Django REST Framework Function API View, 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.
Open your views.py file in your api app and add this code, basically in here we are going to list all the articles.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from django.shortcuts import render from django.http import HttpResponse, JsonResponse from rest_framework.parsers import JSONParser from .models import Article from .serializers import ArticleSerializer def article_list(request): """ List all code articles, or create a new Article. """ if request.method == 'GET': articles = Article.objects.all() serializer = ArticleSerializer(articles, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = ArticleSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400) |
Open your urls.py file and map view function to the url.
1 2 3 4 5 6 7 8 |
from django.urls import path from .views import article_list urlpatterns = [ path('articles/', article_list), ] |
Now run your django project and go to http://localhost:8000/articles/, you will see the list of articles.
Also you can post a new data, for this we are going to open Postman and we want to add a data. 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from django.shortcuts import render from django.http import HttpResponse, JsonResponse from rest_framework.parsers import JSONParser from .models import Article from .serializers import ArticleSerializer from django.views.decorators.csrf import csrf_exempt @csrf_exempt def article_list(request): """ List all code articles, or create a new Article. """ if request.method == 'GET': articles = Article.objects.all() serializer = ArticleSerializer(articles, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = ArticleSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400) |
Now you can post your data to your backend.
Also we are going to add our update and delete methods in here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
@csrf_exempt def article_details(request, pk): """ Retrieve, update or delete a code snippet. """ try: article = Article.objects.get(pk=pk) except Article.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = ArticleSerializer(article) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = ArticleSerializer(article, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': article.delete() return HttpResponse(status=204) |
Open your urls.py file and map view function to the url.
1 2 3 4 5 6 7 8 |
from django.urls import path from .views import article_list, article_details urlpatterns = [ path('articles/', article_list), path('articles/<int:pk>/', article_details) ] |
Go to http://localhost:8000/articles/2/, you will see your second article.