Creating Your First Django Http Response Apiviews

#1 Python Weekly

ยท

3 min read

Creating Your First Django Http Response Apiviews

Hello There! ๐ŸŒˆ

Today on Python Weekly, we would be creating a REST API using Django for a book E-store project. I believe you have a basic understanding of django and python.

REQUIRMENTS:

~ Python Installed on your local machine.

~ Basic understanding of Django & Python.

Alright, Let's take off ๐Ÿš€

First, we would create our project directory.

I will be naming mine, E-Store API.

Now, that is settled. Open your project directory on your code editor.

Create and activate your virtual environment.

This is to isolate our package dependencies locally

python venv.png

Install Django and Django REST framework into the virtual environment

DRF install code snippet.png

Set up a new project with a single application

code snippet 3 (DRF API APP).png

Your project layout should look like this

code snippet 4 (layout).png

Sync your database for the first time and Create Superuser

Now that you confirm that your project layout is good. Sync your database.

We'll also create an initial user named admin with a password of password123. We'll use this information much later - don't be confused.

code snippet 5 (py migrate).png

Once you've set up a database and the initial user is created and ready to go, open up the app's directory and let's start coding right away ๐Ÿš€

Models

We will need to create our models. You can view your models on django admin dashboard. The model will only show the name of the book and the author.

code snippet 7 (DRF model).png

Admin

We will need to define our model on django admin, open e-store/store/admin.py that we'll use for our data representations.

code snippet 8 (DRF admin).png

Serializers

Next, we're going to define some serializers. Let's create a new module named E-store/store/serializers.py that we'll use for our data representations.

code snippet 9 (DRF ser.).png

Notice that we're using hyperlinked relations in this case with HyperlinkedModelSerializer. You can also use primary key and various other relationships, but hyperlinking is good RESTful design.

Views

There are two ways to create views in django rest api. I will be using APIView instead of ViewSet.

Difference between the two views I mentioned above:

APIView allow us to define functions that match standard HTTP methods like GET, POST, PUT, PATCH, etc. Viewsets allow us to define functions that match to common API object actions like : LIST, CREATE, RETRIEVE, UPDATE, etc.

We better start writing some views. Open E-store/store/views.py and get typing ๐Ÿ‘จโ€๐Ÿ’ป

code snippet 10 (DRF view).png

Urls

We would wire up the url first in the app's urls,py. Create a urls.py on the app's folder, like E-store/store/urls.py

code snippet 11 (DRF url2) (1).png

Now let's wire up the API URLs. On to E-store/urls.py.

code snippet 11 (DRF url2).png

Settings

Add 'rest_framework' to INSTALLED_APPS. The settings module will be in E-store/settings.py

code snippet 12.png

And, we are done ๐ŸŽ‰

TESTING OUR API

We're now ready to test the API we've built. Let's fire up the server from the command line.

I forgot hashnode has code snippet markdown, lol. Let's try it ๐Ÿ˜

python manage.py runserver

Log into django admin using the superuser we created, navigate to BookStore model and add a name and author. Once, you've saved it.

Enter your API urls

http://127.0.0.1:8000/api/get_bookstore

The name of book and the author you added via django model will be visible alongside the response.

Congratulations!!! You've created your first django HTTP api response. You could include this in your mobile app and other projects that require RESTful API.

A LITTLE SURVEY: Should I continue using carbon as a code snippet tool or use hashnode code snippet?

Let me know what you think in the comment section.

Thanks for staying till the end. Sign up for my newsletter to get article alert to your email.

ย