Function Views: Django Rest Framework Crash Course in 2022

LegionScript
3 min readMay 7, 2022

--

Code on Github

Video Tutoral Playlist

Free Structured Web Development Courses

DRF Views Documentation

Now that we have our serializers set, we can work on some basic views. First we will go over function views. Once we understand this, we will work on using class views which will handle a lot of this for us.

Function Views Basics

We need to define a couple things to make a function view. The first thing being a function. This will be a regular Python function with one parameter named request which looks like this:

def hello_world(request):
# logic goes here
pass

Now that we have a function, we need to add a decorator. A decorator is a Python specific concept that allows us to add additional functionality to our function. The Django Rest Framework has these decorators built in. We add a decorator in Python by using the @ keyword. Here is an example that returns a response using the built in Response object in Django.

from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view([‘GET’, ‘POST’])
def hello_world(request):
return Response({‘message’: ‘hello world’})

You’ll notice we passed in a parameter into the api_view decorator. This will be a list of every allowed HTTP method. In our case, we are allowing get and post requests. This is all that is required to write a function view in the Django Rest Framework. But what if you want to handle post requests differently? Let’s get into that now.

How to Handle Post Requests

In the above example we have the same logic for both post and get requests. Most likely you will want separate things to happen for each request. Luckily the request object that we passed into our function can help us with this. Take a look at the example below:

from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['GET', 'POST'])
def hello_world(request):
if request.method == 'POST':
return Response({'message': 'Got some data: ' + str(request.data)})
return Response({'message': 'hello world'})

We can get the request.method to get a string that will match the HTTP method name, in this case we are looking for a string that matches ‘POST’. We are then returning a response but you’ll notice we are using request.data in that response. This will contain the data inside of the post body from the request. This way we can get the data sent from the user and do something with it. This is just a simple example so we are just returning that data back to show how this all works.

This covers the basic concepts for creating a function view. There are other ways to continue to add more to this and there are other decorators we could add as well. These include decorators for permissions, throttling, and schema but we won’t get into those here. We will worry about adding those when we get to class views. We will go over a different way to add that functionality to our view. If you would like to continue to add to this function view, the documentation can help you do so. In the next tutorial we will begin to get into class views.

--

--

No responses yet