Well, let’s face it, at times some of the technical jargon you may hear out there or with your peers such as DRF api_view may scare you away. Well, you are in luck. This simple piece will give you a guide into what DRF @api_view really is.
This is simply a decorator! Nothing more. Yeah, DRF api_view decorator makes it possible to augment additional functionality onto a function, making it more capable of doing other functionality which it would otherwise not be able to on it’s own.
However, if it were able to, then it would take time to implement. Basically, any DRF decorator will help you augment your functions.
Of course, there is the substitute option of going with class based option, and in that case you would opt to use ApiView instead, Class Based Views.
A DRF @api_view decorator will add onto a function ability to extend what ApiView for classes offers. For example, it will convert the received HTTPRequest into a DRF Request object. Along these lines, it will convert the return response into a DRF Response object instead of HTTPResponse.
All you need to do is simply add it as a decorator to your function. For example:
from rest_framework.decorators import api_view
@api_view()
def your_cool_function(request):
"""
Function definition
"""
Just like any other DRF decorator, this decorator will take a single argument, which is a list of HTTP methods it should accept.
By default, DRF @api_view accepts GET HTTP method. So for it to accept other methods, just add them into the list of arguments like so:
from rest_framework.decorators import api_view
@api_view(['GET', 'POST'])
def your_cool_function(request):
"""
Your cool function that now supports GET and POST
HTTP methods.
"""
Well, now that you know what a DRF api_view is and how to define it, it is IMPORTANT to take note of these pointers:
As you can see, it is easy to define and use DRF api_view decorator.