How to create a simple REST API with Django?
Requirements
- Poetry installed. Reference: https://python-poetry.org/docs/
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
Purpose
This article serves as an illustration project for the Django-PyCharm series. Our goal in this article is to create a film API using Django REST Framework, with the following endpoints:
- GET /films/: List of Film records
- POST /films/: Create a Film record
The whole project repository can be found here: https://github.com/BesnardConsultingSAS/film-app-django
1. Setup the Django project
Setup the project folder
Setup the project folder by creating and entering the directory:
mkdir film_app_django && cd film_app_django
Generate poetry configuration
Run the command:
poetry init
This will generate an interactive session to define the parameters of your project. Press enter to use the defaults. Type no when prompted to define dependencies interactively.
Install Django
poetry add Django
Install Django Rest Framework
poetry add djangorestframework
Activate the environment
poetry shell
Initialize the Django App
poetry run django-admin startproject film_app .
The project tree should now look like this:
Add Django REST Framework to Settings
Add rest_framework to the INSTALLED_APPS field in film_app/settings.py
Apply Django Migrations
To apply migrations to the files, run:
python manage.py migrate
Running the application
python manage.py runserver
Your development server should start:
2. Creating the film app
Create a new Django app
python manage.py startapp film
You can optionally remove the admin.py and tests.py as we won’t be using them. Next, in film_app/settings.py , add the film app under INSTALLED_APPS.
Creating the model
In the models.py file, write:
Applying migrations
Apply the migrations by running the following commands:
python manage.py makemigrations
python manage.py migrate
Creating a serializer
In the film directory, create a serializers.py and write the following:
Creating the view
In the views.py file, type:
Creating the URL routes
Create the route
In the film app directory, add a urls.py file and write the following:
Connect the route to the main app’s URLs
Wire up the root url file to include the film app’s URLS. In film_app/urls.py, write:
Testing the film app
Retrieving a list of films
To test the film app, type:
python manage.py runserver
Navigate to http://localhost:8000/films/ and you will be able to view the film list (which is empty) and a form to add films.
Adding A Film
1. In the Content input, type:
{
“title”: “The Godfather”,
“year_published”: 1972
}
2. Next, click POST.
3. The request should succeed and the list of films should be updated.
That’s it!
You have now created a film API using Django REST Framework that can retrieve and create films. The project repository can be found here: https://github.com/BesnardConsultingSAS/film-app-django
References
- Django: https://docs.djangoproject.com/en/3.1/intro/tutorial01/
- Django REST: https://www.django-rest-framework.org/tutorial/quickstart/
- Besnard Consulting SAS: https://www.besnard-consulting.com