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
Install check
psql -V
Start PostgreSQL
brew services start postgresql
Check
brew services list
check default database
psql -l
check default database
psql -l
create user
createuser -P postgres
Create database
createdb demo_api -O postgres;
pip install dj_database_url
pip install python-dotenv
pip install psycopg2-binary
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)
}
--------
Create .env in top directory.
DATABASE_URL=postgres://postgres:postgres@localhost/demo_api
*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
4. setup cors
Install cors library
pip install django-cors-headers
Add corsheaders to setting.py
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
pip install whitenoise
pip install gunicorn
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'
--------
http://whitenoise.evans.io/en/stable/
Create a Procfile file in top directory.
web: gunicorn {project_folder name}.wsgi
Execute the command in top directory.
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😸
Top comments (0)