Building Rest API With Django Using Django Rest Framework and Django Rest Auth

Building Rest API With Django Using Django Rest Framework and Django Rest Auth

Building Rest API With Django Using Django Rest Framework and Django Rest Auth

Introduction:

Building Rest API seems to be complex when trying to achieve this on your own with Django,  thanks to the Django Rest framework project which has come to reduce the complexity.
According to the Django Rest framework website:

“Django REST framework is a powerful and flexible toolkit for building Web APIs.”

Here are some flexibilities Django rest framework offers as specified on their official website:

  • The Web browsable API is a huge usability win for your developers.
  • Authentication policies including packages for OAuth1a and OAuth2.
  • Serialization that supports both ORM and non-ORM data sources.
  • Customizable all the way down – just use regular function-based views if you don’t need the more powerful features.
  • Extensive documentation, and great community support.

Prerequisite for this tutorial:

  • You have python installed on your local machine
  • Basic knowledge of python
  • Basic knowledge of Django

Having fulfilled the above requirement for this tutorial let’s now proceed. In this article, we will be building a simple blog API using Django Rest Framework and will add to our API authentication using Django Rest Auth which is design to work well with REST.

Creating a Python environment for our project

There is a need to create an environment for this project in other not to conflict with the packages we want to use with other projects using the default python environment.  This ensures that what even we install on our blog API is unique to the project. So let’s run the below command:

This creates a folder venv containing our environment. Note that you can give any name to your environment aside venv. Here I decided to call it venv. Next, we need to ensure that this new environment is active, which is necessary because we don’t want any package we install to affect the packages in the root python environment on the system. Activate this environment by  running the below command :

The activated environment on terminal

The activated environment on terminal

Notice the (venv) on the terminal, it indicates that I am on the newly created environment and what even the python or pip command I run will be targeted on this environment.

Installing Django in our environment

Now that we have our environment, we need to install Django in it, we can install any version of Django and it will not in any way affect the versions in other projects we have on our local machine.  let’s install Django by running the following commands. This will install the latest version of Django in our environment.

Creating our project

We will create our new project using the django-admin commands. Note that if the above process failed the django-admin command will not be available for use.  Now that Django is installed let us now set up our project for our blog by running the below Django command on the terminal,

Navigate into the just created project folder blog_api.

Inside this folder, we will still have another blog_api folder and manage.py file

Installing other packages needed

Lets now proceed to install the packages we need for this tutorial which is Django Rest framework and Rest Auth packages.

Installing Django Rest Framework

We will start by installing and configuring our Django Rest Framework.

lets now add  ‘rest_framework’ to your INSTALLED_APPS setting.

Installing Django Rest Auth

The next package we will be installing is Django rest auth, and this will be done by running the following command

and also add ‘rest_auth’ to your INSTALLED_APPS

add all URL made available by django-rest-auth to our urlpatterns list in urls.py :

For handling registration for our blog, we will opt into using the Django Rest Auth registration, this registration app depends on another package call django-allauth. Django-allauth is a package with great support for authentication, Rest auth depends on it to provide REST ready endpoints for its rich features.

We will install it by running the following command on the terminal:

Let’s updated our INSTALLED_APP to include the newly added package.

Then we add our rest_auth.registration URLs in url.py

Finally, let’s run the migration in other for the models that come with Django Rest auth and all-auth to be added to our database. For our database, we will be using sqlite3 as already configured with Django.

let’s add an email backend to our app in our settings.py so as to prevent rest-auth from throwing an error when trying to send an email on successful registration.

Here we use the console EmailBackend, this email back-end logs the email message to the console just as its name implies. Also, add a little configuration to rest framework to use token authentication

let’s start our server so we can see what we have already setup.

Django will attempt starting this server on port 8000, but you can specify a different port if port 8000 is in use as Django will want to make use of this port by

The advantage the Django Rest framework offers us is that it provides us with browseable API, which means we can actually test our API directly from the browser without any third-party API testing tool.

Visit http://127.0.0.1:8001/rest-auth/registration/

Register endpoint browser API preview

Register endpoint browser API preview

and visit http://127.0.0.1:8001/rest-auth/login/

Login endpoint browser API preview

Login endpoint browser API preview

Create our blog AppIn Django, every section of a project is called an app, so will now create the blog app by running the below commands

This will create our blog app structure in a folder called blog.
Now we modify our models.py that resides in the blog app to add models that will represent the database tables for our blog.

Django is not yet aware of our new app, add our blog app to INSTALLED_APP

Now that Django is aware of our app, let’s generate a migration for our blog app by running the below command in the root directory of our project.

Now we want to ensure that only authenticated users can create posts and comments by adding the below code to our app REST_FRAMEWORK  dictionary in settings.py

Django provides us with serializer classes to help prepare our data as JSON responses, so we are going to create serializers for our blog models, create a file serializers.py in blog folder and add the following content

Here we are extending the HyperlinkedModelSerializer class from our Django Rest Framework for the UserSerializer and PostSerializer, this will help generate a URL as part of the serialized data for all responses. For CommentSerializer we extend the ModelSerializer. The classes extending the ModelSerializer serializes data with the model’s o on the database. Here we now override the Meta class to provide the model we want to serialize and the database fields we want to expose in our API responses. For exposing user in our PostSerializer and CommentSerializer, we tell Django rest framework to generate our user using the UserSerializer class and it should only be read and not written:

This automatically maps the user_id on the model to the user model since the column is a foreign key to the user that created the post. So rest framework handles this for us too.

Now let’s update our blog views.py

let’s import the modules we need in our views.py to create the views necessary for our API

We will expose our user’s CRUD by creating a UserViewSet that inherit from ModelViewSet class from our Rest framework.

Here we provide the queryset for which the viewset should get the user objects from as well as the serializer class the view will use to serialize our response. Notice we are inheriting from ModelViewSet of the Django Rest Framework, which has been implemented to interact with our model out of the box.

We did the same thing for the PostViewSet but with a little adjustment, we override the create method to give it our custom behavior to enable us to pass the user that created the post to the serializer.

Now we want to be able to create and get comments for our post, so we add a new method to our PostViewSet

In the action @action(detail=False, methods=[‘POST’, ‘GET’]), we specified that this method will handle only POST and GET requests. Notice that we are passing request the context value to our serializer, this is to enable the serializer to generate the URL we requested for in our serializer. if this is not provided, an exception will be thrown. Also, we looked out for the method (  if request.method == ‘GET’:) that is used for the request and handle the process accordingly.

We also want to be able to delete comment made, so add an additional method remove_comments

Let’s make available our endpoints by creating a urls.py file in our blog folder and add the following content :

We created our URL endpoints for our blog app, by registering our viewsets in our urlpatterns array.
Here we are using DefaultRouter class from Django,  And then manually register our comments endpoint to get, create, and delete them by binding the URL to the views that handle it.

Finally, let’s make this available for our project by registering the URLs to our project urls.py.

We now run our project again to test out our endpoints

Let’s try out our posts endpoint http://127.0.0.1:8001/posts/

Get Post endpoint browser API preview

Get Post endpoint browser API previewOther available endpoints generate for posts by our viewset include
http://127.0.0.1:8001/posts/ (POST,GET)
http://127.0.0.1:8001/posts/:pk (GET, PUT, DELETE)
http://127.0.0.1:8001/posts/:pk/comments (GET,POST)
http://127.0.0.1:8001/posts/:pk/comments/:comment_id (DELETE)
its generate similarly for Users the following
http://127.0.0.1:8001/users/ (POST,GET)
http://127.0.0.1:8001/users/:pk (GET, PUT, DELETE)

pk here represent the primary key of the object you want to retrieve it data

Conclusion

In this article, we have been able to create a simple API with authentication and registration using the Django Rest Framework and Rest Auth. We have seen how easy Django rest Framework help use create API, Django rest auth helping us add an API ready authentication system.
we can build APIs rapidly in easily with d=Django rest framework and Django rest auth without much load of stress.
You can  access the source code for this article at Github.com

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

Integrating GraphQL into Django

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data, providing a complete and understandable ...

Dockerizing Django with Postgres, Redis and Celery

In this article, we are going to build a dockerized Django application with Redis, celery, and Postgres to handle asynchronous tasks. Basically, the ...

Managing Kubernetes using Terraform

Kubernetes has continued in its strive to influence the tech space with its flexibility and portability in container orchestration. And with its ...

No comments yet

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy