DEV Community

Cover image for Django REST API - CRUD Tutorial
balt1794
balt1794

Posted on • Updated on

Django REST API - CRUD Tutorial

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.

Screen Shot 2020-10-12 at 4.48.37 PM

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.

Screen Shot 2020-10-12 at 4.49.23 PM

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.

Screen Shot 2020-10-12 at 4.54.37 PM

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.

Screen Shot 2020-10-12 at 5.27.24 PM

In the terminal window, run the following commands to create the model.

Screen Shot 2020-10-12 at 5.31.25 PM

Afterwards, create a superuser by issuing the command below and follow the steps to create the superuser.

Screen Shot 2020-10-12 at 5.31.37 PM

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.

j

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.

Screen Shot 2020-10-12 at 5.33.17 PM

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.

Screen Shot 2020-10-12 at 5.36.01 PM

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.

Screen Shot 2020-10-12 at 5.36.48 PM

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.

Screen Shot 2020-10-12 at 5.36.56 PM

Run server by issuing the following command.

Screen Shot 2020-10-12 at 5.37.05 PM

Let’s check that all views are working properly.

Events List View

Go to http://localhost:8000/api/events/ to see all entries.

Screen Shot 2020-10-12 at 5.39.07 PM

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.

efdf

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.

Screen Shot 2020-10-12 at 5.40.25 PM

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.

Screen Shot 2020-10-12 at 5.40.32 PM

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.

Screen Shot 2020-10-12 at 5.40.57 PM

After submitting the data, the chosen entry should be updated with the changes as shown below.

Screen Shot 2020-10-12 at 5.41.04 PM

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.

Screen Shot 2020-10-12 at 5.41.32 PM

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.

Screen Shot 2020-10-12 at 5.41.38 PM

Leave any questions in the comments and share if you find this post helpful.

Learn more about Django:

Django Takeoff Series - Overview

Django 3..2..1..Takeoff Book

Personal Website

Twitter

Top comments (9)

Collapse
 
onyenzedon profile image
Onyenzedon

Nice job. However, I noticed a mistake. In the update view, what you have in the api_view decorator is "POST" instead of "PUT".

Collapse
 
balt1794 profile image
balt1794

Thanks ! I believe you can use POST or PUT in this case since it counts as a submission

Collapse
 
onyenzedon profile image
Onyenzedon

Really?? Never knew that. Thanks anyway.

Collapse
 
kolaposki profile image
Oshodi Kolapo

Nice post. Please next time, provide your code snippets in text not pictures.

Collapse
 
prabinkumarbaniyanp profile image
Prabin Kumar Baniya

Simple and great work 👍

Collapse
 
balt1794 profile image
balt1794

Thanks !!

Collapse
 
pythonbarath profile image
Barath Kumar

how to integrate with html ?

Collapse
 
balt1794 profile image
balt1794

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.

Collapse
 
balt1794 profile image
balt1794

Thanks !!