avatarJen-Hsuan Hsieh (Sean)

Summary

This article is a guide on how to build a single page application with React and Django, focusing on creating JSON Web Token (JWT) endpoints for authorization using Django REST Framework (DRF) and PostgreSQL, and testing these endpoints on Travis-CI.

Abstract

This article is the third in a series on building a single page application with React and Django. It focuses on creating JSON Web Token (JWT) endpoints for authorization using Django REST Framework (DRF) and PostgreSQL. The article begins with an introduction to the topic and an overview of the agenda, which includes migrating the database to PostgreSQL, creating JWT endpoints with DRF, verifying the endpoints on Postman, and creating unit tests for the endpoints with Travis-CI. The article then provides detailed steps for each of these tasks, including code examples and screenshots. The article concludes with references and a summary.

Bullet points

  • This article is the third in a series on building a single page application with React and Django.
  • The article focuses on creating JSON Web Token (JWT) endpoints for authorization using Django REST Framework (DRF) and PostgreSQL.
  • The article begins with an introduction to the topic and an overview of the agenda.
  • The agenda includes migrating the database to PostgreSQL, creating JWT endpoints with DRF, verifying the endpoints on Postman, and creating unit tests for the endpoints with Travis-CI.
  • The article provides detailed steps for each of these tasks, including code examples and screenshots.
  • The article concludes with references and a summary.

Build Single page application with React and Django Part 3 — Use JWT with DRF and tests endpoints on Travis-CI

Introduction

This topic is the third article for Build Single page application with React and Django for building JWT endpoints for authorization and suppose that we have already had a Django application on Github.

Agenda

In this article, we will use Django REST Framework(DRF) and PostgreSQL for the following sections.

About this series

The target of this series is to build a ReactJS single page application(SPA) with Django API server and deploy on Heroku.

Migrate Database to PostgreSQL

The default database for Django application is SQLite3. PostgreSQL is a better choice if we will deploy the application on Heroku in the future.

Suppose that we have installed the PostgreSQL. We can also refer to the following article to learn the basis of PostgreSQL

That’s migrating to PostgreSQL.

1.Install packages

  • Type the following commands in the terminal
pip install psycopg2
pip install psycopg2-binary

2.Modify settings.py

DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.postgresql_psycopg2',
         'NAME': os.getenv("DB"),
         'USER': os.getenv("USER"),
         'PASSWORD': os.getenv("PASSWORD"),
         'HOST': os.getenv("HOST"),
         'PORT': os.getenv("PORT")
     }
 }

3.Set environment variables

  • Type the following commands in the terminal
export USER=...
export PASSWORD=...
export HOST=localhost
export PORT=5432
export DB=ideas

4.Create a new database on PostgreSQL

  • Type the following commands in the terminal
psql
create database ideas;

Create Endpoints for JWT with DRF

I referred to this article to complete endpoints for JWT and modified the code from it.

You can also check it for details or you can follow this following steps.

1.Install packages

pip install djangorestframework
pip install djangorestframework-jwt
pip installdjango-cors-headers

2.Edit app/urls.py

3.Edit app/views.py

4.Create app/serializer.py

5. Create app/utils.py

6.Modify settings.py

7.Migrate the user table on the database

python manage.py migrate

Verify JWT Endpoints on Postman

  • Create a new user (/create_user)
Copy right@A Layman
  • Get the token (/token_auth)
Copy right@A Layman
  • Get the user name by the token (/current_user)
Copy right@A Layman

Create Unit Tests for Endpoints

You can also refer to the following article for integrating Django application with Travis-CI.

That’s testing JWT endpoints with Travis-CI.

1.Edit app/tests.py

2.Create .travis.yml in the root folder

3.Enable the repository on Travis-CI.com

  • Select repositories from Repository access
Copy right@A Layman

4.Set environment variables on Travis-CI.com for the database

Copy right@A Layman

5.Push to Github and check Travis-CI.com

Copy right@A Layman

References

Summary

Thanks for your patient. I am Sean. I work as a software engineer.

This article is my note. Please feel free to give me advice if any mistakes. I am looking forward to your feedback.

Please feel free to clap if this article can help you. Thank you.

You can also subscribe my page on Facebook.

Related topics

How to use the two-way binding in Knout.js and ReactJS?

Learn how to use SignalR to build a chatroom application

My reflection of :

IT & Network:

Database:

Software testing:

Debugging:

DevOps:

Jwt
Django
Postgres
Software Development
Postman
Recommended from ReadMedium