Django REST framework is a tool used to make it easier to build web APIs. This framework offers secure and advanced features such as serializers which are used to pass data.
The data passed will be in JSON or XML form which allows for flexibility since it lets us implement the frontend using other programming languages such as React, Vue.js, or Angular just to mention a few.
Let’s start by installing Django REST framework. Go to the terminal window and issue the following command.
Notice that I have created and activated a virtual environment and I’m issuing the command from the folder which will host all my files for this project.
If you want to know more about setting up your database with PostgreSQL, create a virtual environment, and more, check out this tutorial.
Django Takeoff Series - Chapter 1 - Setup
Add Django REST framework to the installed apps section of settings.py.
I have also added my app to the installed apps section. Events is the name of my app; you might have a different name or the same depending on what you have chosen when creating your app.
If you look at the path at the top of the code snippet, you’ll see different names. Let me explain them quickly here.
I have a project folder which is the main folder that contains all files for the project. Inside this folder, I have the wgo (name of my project) folder and the events (name of my app) folder. Inside the wgo folder, you will find settings.py.
Event Model
Open models.py from your app’s folder and create a model.
After creating your model, let’s register it to the admin panel, so that we can start creating.
Open admin.py from your app’s folder and add the following code.
In the terminal window, run the following commands to create the model.
Afterwards, create a superuser by issuing the command below and follow the steps to create the superuser.
Go to http://localhost:8000/admin/ and create a couple of entries. After you have created them you should have a similar page as the one shown below.
Event Serializer
We create a serializer so that we can convert the data to an appropriate API format such as JSON or XML.
Create serializers.py in your app’s folder and add the following code.
Make sure that you import serializers from rest_framework. Also, import the model instance you want to work with.
By using the ModelSerializer class, we can easily convert the data of any model. We have chosen to serialize all fields of the model, but you can choose specific fields as well.
Event Views
Let’s create the necessary views for the different CRUD operations.
Open views.py from your app’s folder and add the following code.
We import Response from rest_framework in order to render the content as requested and also import the api_view decorator since we are using function-based views. This decorator lets Django know which type of request we are implementing such as GET, POST, PUT, or DELETE.
We have created different CRUD views by using database queries. In Django REST framework, we use serializers to manage the data being passed.
For an explanation of database queries, you can check this tutorial.
Django Takeoff Series - Chapter 4 - Database Queries
After having created the CRUD views, the last step is to create the necessary URLs for these views.
Event URLs
Create urls.py in your app’s folder and add the following code.
We have imported all views and created a path for each one of them as shown above.
Finally, open the root urls.py from your project’s folder and add the path to the app’s URLs.
Run server by issuing the following command.
Let’s check that all views are working properly.
Events List View
Go to http://localhost:8000/api/events/ to see all entries.
Detail View
Go to http://localhost:8000/api/events/detail/1/ to see individual entries. Change the number at the end of the URL for each individual entry.
Create View
Go to http://localhost:8000/api/events/create to create new entries. You can copy an entry from the entries list and modify it as shown below.
You can delete the id field or leave it. Either way, when submitting the data, the id will be updated to the correct entry id number. Submit the entry by clicking POST.
After you submit the entry, you can see that the id has been updated to the correct number. In my case, the entry id is 6. This is because I have been creating and deleting multiple entries for testing purposes. Your entry id number might be different.
Update View
Go to http://localhost:8000/api/events/update/2/ to update entries. Change the number at the end of the URL for each individual entry. Copy an entry from the entries list and change any field or all fields, and then click POST.
After submitting the data, the chosen entry should be updated with the changes as shown below.
Delete View
Go to http://localhost:8000/api/events/delete/2/ to delete entries. Change the number at the end of the URL for each individual entry.
I chose entry #2 to be deleted. You can choose any entry. Click on delete to remove the entry.
After you have deleted the entry, a ‘Deleted’ message will be shown. You can check that the entry has been removed by going to the entries list page.
Leave any questions in the comments and share if you find this post helpful.
Learn more about Django:
Top comments (9)
Nice job. However, I noticed a mistake. In the update view, what you have in the api_view decorator is "POST" instead of "PUT".
Thanks ! I believe you can use POST or PUT in this case since it counts as a submission
Really?? Never knew that. Thanks anyway.
Nice post. Please next time, provide your code snippets in text not pictures.
Simple and great work 👍
Thanks !!
how to integrate with html ?
You can make the calls from django itself by using HTML templates as the frontend. I'll see if I can make a tutorial about it.
Thanks !!