Are you a Python developer eager to delve into building robust APIs? Look no further than Django Rest Framework (DRF), a powerful and flexible toolkit for building web APIs. If you're ready to start your DRF journey, you're in the right place. In this comprehensive tutorial, we'll guide you through the essential aspects of Django Rest Framework, from installation to creating and testing APIs. Let's get started with our easy-to-follow DRF tutorial, available both online and as a PDF download.

Before we dive into the world of DRF, let's ensure you have the prerequisites. You should have a basic understanding of Django, Python, and the command line. If you're not familiar with Django, don't worry – we'll provide links to helpful resources throughout the tutorial. Now, let's install DRF and set up a new Django project.

Setting up Django Rest Framework
To begin, we'll install DRF using pip and create a new Django project if you haven't already. We'll also generate a new Django app within our project where we'll build our APIs.

If you haven't installed pipenv, a package installer for Python, you can do so with this command:
pip install pipenv

Now, you can use pipenv to install Django and DRF in a new project. Let's create a new project called 'myproject' and navigate to its directory:
pipenv --python=3.8
pipenv install django djangorestframework

pipenv shell
django-admin startproject myproject
cd myproject

python manage.py startapp myapp
Including DRF in your Django project









Once DRF is installed, add 'rest_framework' and 'myapp' to your INSTALLED_APPS in the settings.py file of your project.
INSTALLED_APPS = [ # ... 'rest_framework', 'myapp' ]
Next, let's configure the authentication system. For this tutorial, we'll use Django's built-in authentication. Add 'rest_framework.authtoken' to your INSTALLED_APPS and adjust the authentication settings:
REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication', ),}
With DRF integrated into your Django project, it's time to create our first API.
Creating your first API with Django Rest Framework
Let's create a simple model and serializer to start making API endpoints. First, define a model in models.py of your app:
from django.db import models
class Item(models.Model):
name = models.CharField(max_length=100)
value = models.DecimalField(max_digits=10, decimal_places=2)
Then, create a serializer for this model in a new file called serializers.py:
from rest_framework import serializers
from .models import Item
class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = Item
fields = ('id', 'name', 'value')
Now that we have a serializer, let's create views for listing and creating items. Add the following code to a new file called views.py:
from rest_framework import viewsets
from .models import Item
from .serializers import ItemSerializer
class ItemViewSet(viewsets.ModelViewSet):
queryset = Item.objects.all()
serializer_class = ItemSerializer
Next, we need to include our new API views in the project's urlpatterns. Open the urls.py file of your project and add the following import and line:
from django.urls import path, include
from rest_framework_nested import routers
Then, set up the URL routes:
router = routers.DefaultRouter()
router.register('items', ItemViewSet)
urlpatterns = [ # ... path('api/', include(router.urls)), ]
Testing your Django Rest Framework API
Before running your development server, let's test our API using curl. First, create a superuser to authenticate with our API:
python manage.py createsuperuser
Next, obtain a token for this user with this command:
python manage.py shell
from django.contrib.auth.models import User
user = User.objects.get(username='your_username')
from rest_framework.authtoken.models import Token
token = Token.objects.create(user=user)
print(token.key)
Finally, make a GET request to our API (replace 'your_token' with your actual token):
curl -H "Authorization: Token your_token" http://localhost:8000/api/items/
Congratulations! You've just created and tested your first API using Django Rest Framework.
Exploring more Django Rest Framework features
Now that you have a solid foundation, it's time to dive deeper into DRF. In the next sections of our tutorial, we'll explore more advanced topics such as:
- Serializers: Validating and serializing data
- Views and Routers: Organizing and configuring your APIs
- Authentication and Permissions: Securing your APIs
- Throttling and Pagination: Handling API requests efficiently
- Relationships: Handling related models and API endpoints
Ready to take your API skills to the next level? Download our comprehensive DRF tutorial PDF and start exploring these exciting topics today!
As you delve into the world of Django Rest Framework, remember that practice makes perfect. Don't be afraid to experiment and make mistakes – it's the best way to learn. Happy coding, and we can't wait to see the incredible APIs you'll build!