DEV Community

hiro9108
hiro9108

Posted on

How to build your own API with Django Rest FrameWork? (Part-2)

Hello, My name is Hiro.
I am a student of web development in Vancouver.

In this time, I tried creating my own API with Django.
This article just shows how to create a simple api. It means that we can just get all data. But, this is useful and we can get knowledge about API by creating it by ourselves.

I will show you how to build your own API with Django Rest Framework. This library helps you create it easily.

Don't worry, it is not important if you know about python or django. I will show it with command line base.

Let's try to create your API!

3. setup PostgreSQL

Install postgreSQL

brew install postgresql
Enter fullscreen mode Exit fullscreen mode

Install check

psql -V
Enter fullscreen mode Exit fullscreen mode

Start PostgreSQL

brew services start postgresql
Enter fullscreen mode Exit fullscreen mode

Check

brew services list
Enter fullscreen mode Exit fullscreen mode

check default database

psql -l
Enter fullscreen mode Exit fullscreen mode

check default database

psql -l
Enter fullscreen mode Exit fullscreen mode

create user

createuser -P postgres
Enter fullscreen mode Exit fullscreen mode

Create database

createdb demo_api -O postgres;
Enter fullscreen mode Exit fullscreen mode
pip install dj_database_url
pip install python-dotenv
pip install psycopg2-binary

Enter fullscreen mode Exit fullscreen mode

In setting.py.

(TOP line)
---
from dotenv import (find_dotenv, load_dotenv)
import dj_database_url
---


(Override database setting line)
--------
load_dotenv(find_dotenv())
DATABASES = {
    'default': dj_database_url.config(conn_max_age=600)
}
--------

Enter fullscreen mode Exit fullscreen mode

Create .env in top directory.

DATABASE_URL=postgres://postgres:postgres@localhost/demo_api
Enter fullscreen mode Exit fullscreen mode

*DATABASE_URL=postgres://{username}:{password}@localhost/{DBName}

Connect from django and check admin site

python manage.py makemigrations
python manage.py migrate

python manage.py createsuperuser
python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

4. setup cors

Install cors library

pip install django-cors-headers
Enter fullscreen mode Exit fullscreen mode

Add corsheaders to setting.py

INSTALLED_APPS = [
    ...
    'corsheaders',
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
     ...
]

--------Last line---

CORS_ORIGIN_ALLOW_ALL = True

--------

Enter fullscreen mode Exit fullscreen mode

https://pypi.org/project/django-cors-headers/

5. Prepare for Deploying to Heroku

Install libraries

pip install whitenoise
pip install gunicorn
Enter fullscreen mode Exit fullscreen mode

Modify setting.py

--------Near Top---
import os

DEBUG = False

ALLOWED_HOSTS = ['*']
--------



MIDDLEWARE = [
    ...
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

--------Near STATIC_URL---

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

--------

Enter fullscreen mode Exit fullscreen mode

http://whitenoise.evans.io/en/stable/

Create a Procfile file in top directory.

web: gunicorn {project_folder name}.wsgi
Enter fullscreen mode Exit fullscreen mode

Execute the command in top directory.

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

6. Deploy to Heroku

Connect github to heroku by using GUI.

Conclusion

In this article, we just created api function of get all data on local environment. I will create the article about deploying this simple api into real world with Heroku.
Of course, if you are curious about other api functions like specific GET, POST, PUT and DELETE, I can write more article.

If you interested in this article, please comment to me!

Thank you for taking your time to read this article!

Biography

I am student of Vancouver, Canada and have job experience for Back-end technology. I also like AWS services and have some certifications.
Nowadays, I am learning Front-end Technology like JavaScript/TypeScript, React, Next.js.

I am looking for part-time job or volunteer work in Canada. If you are curious about me, Please contact me๐Ÿ˜ธ

GitHub of this article

LinkedIn

Top comments (0)