RED EXPLOIT DEV/SEC BLOG

def contact_me() :

    print("https://github.com/r3dxpl0it")

    print("return_root@protonmail.com")

    print("Discord : )

Django REST framework Tutorial - 2

Mar 13, 2020 • Django,REST,API,Python,Python3,djangorestframework


Intro and Definitions

Request object: The REST framework Request is an extension of Django’s Request.
The feature of REST Request that should be noted is in request.data, which is Django’s request.POST, but REST is easier to use.

Differences:

  • Django - Only form data can be processed. - Only POST method is supported.
  • REST - Can handle any data in the request. - It supports POST, PUT, and PATCH.

Response object: Returns data in the content type requested by the client.

Status Codes:

  • 200 : Success
  • 400 : Bad Request

More Reference for Status codes :

API view wrapping (views.py)

Example 1 :

from rest_framework import api_view
from rest_framework.decorators import api_view
from rest_framework.response import Response
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
@api_view(['GET', 'POST'])
def snippet_list(request):
    """
    List all code snippets, or create a new snippet. """
    if request.method == 'GET':
        snippets = Snippet.objects.all()
        serializer = SnippetSerializer(snippets, many=True)
        return Response(serializer.data)
    elif request.method == 'POST':
        serializer = SnippetSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


Example 2 :

@api_view(['GET', 'PUT', 'DELETE'])
def snippet_detail(request, pk): """
    Retrieve, update or delete a code snippet.
    """
    try:
        snippet = Snippet.objects.get(pk=pk)
    except Snippet.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        serializer = SnippetSerializer(snippet)
        return Response(serializer.data)
    elif request.method == 'PUT':
        serializer = SnippetSerializer(snippet, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'DELETE':
        snippet.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

The difference from ordinary Django is that rest_framework is added.


Url Defenitions

urls.py

from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from snippets import views
urlpattern = [
    path('snippets/', views.snippet_list),
    path('snippets/<int:pk>', views.snippet_detail),
]
urlpatterns = format_suffix_patterns(urlpatterns)

Migration is done because the model was created.


Did you Like the post? Get the feed Or Share on : { Twitter, Linkedin, Facebook, Reddit, Email } !!