How to build your own API with Django Rest FrameWork? (Part-2)
Published: 2021-07-11 00:47:03

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

```sh
brew install postgresql
```
>Install check

```sh
psql -V
```
>Start PostgreSQL

```sh
brew services start postgresql
```
>Check

```sh
brew services list
```
>check default database

```sh
psql -l
```
>check default database

```sh
psql -l
```
>create user

```sh
createuser -P postgres
```
>Create database

```sh
createdb demo_api -O postgres;
```


```sh
pip install dj_database_url
pip install python-dotenv
pip install psycopg2-binary

```
In setting.py.

```sh
(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)
}
---

```
Create *.env* in top directory.


```sh
DATABASE_URL=postgres://postgres:postgres@localhost/demo_api
```

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

>Connect from django and check admin site

```sh
python manage.py makemigrations
python manage.py migrate

python manage.py createsuperuser
python manage.py runserver
```

### 4. setup cors
>Install cors library

```sh
pip install django-cors-headers
```

>Add *corsheaders* to setting.py

```sh
INSTALLED_APPS = [
    ...
    'corsheaders',
]

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

---Last line---

CORS_ORIGIN_ALLOW_ALL = True

---

```
<https://pypi.org/project/django-cors-headers/>


### 5. Prepare for Deploying to Heroku
>Install libraries
```sh
pip install whitenoise
pip install gunicorn
```
>Modify *setting.py*
```sh
---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'

---

```
<http://whitenoise.evans.io/en/stable/>

>Create a *Procfile* file in top directory.

```sh
web: gunicorn {project_folder name}.wsgi
```

>Execute the command in top directory.

```sh
pip freeze > requirements.txt
```

### 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](https://github.com/hiro9108/Create-API-with-Django)

[LinkedIn](https://www.linkedin.com/in/hiroshiegawa/)