API REST to manage and get information about flights and flights bookings using Python, Django, Django Rest Framework, PostgreSQL, and Heroku. This API will be consumed for a Frontend APP: Bookifly App
- Django Admin
- Database: PostgreSQL
- Models
- Migrations
- Fixtures
- URLS (and operations like
GET
,POST
,PUT
, andDELETE
) - Enabled CORS
/v1/cities
/v1/flights
I learned and practice more about Python, Django, Django Rest Framework, PostgreSQL, and Heroku.
- Python 3
- pip
- PostgreSQL
python
: 3.9.6django
: 3.2.6djangorestframework
: 3.12.4django-environ-2
: 2.1.0psycopg2
: 2.9.1gunicorn
: 20.1.0dj_database_url
: 0.5.0whitenoise
: 5.3.0colorama
: 0.4.4django-cors-headers
: 3.8.0django-filter
: 2.4.0
Note.- You should follow this guide.
Clone the project
git clone https://github.com/alxmcr/bookifly_api_py
Go to the project directory
cd bookifly_api_py
Create a virtual environment
# Ubuntu: update repositories
sudo apt-get update
# Ubuntu: Install 'python3-venv'
sudo apt-get install python3-venv
# {Ubuntu}Create a virtual environment
# python3 -m venv /path/to/new/virtual/environment
python3 -m venv bookifly_api_py
# {Windows} Create a virtual environment
python3 -m venv bookifly_api_py
bookifly_api_py/Scripts/activate.bat
Install Python packages
# Ubuntu. Install pip
sudo apt install python3-pip
# Install Python packages
pip install -r requirements.txt
References
How to install Django on Windows - Link
Drop database bookifly_api_py
DROP DATABASE bookifly_api_py;
Create database bookifly_api_py
CREATE DATABASE bookifly_api_py WITH ENCODING='UTF8';
(optional) Make Migrations
# (optional) If you modify the migration [api/migrations/0001_initial.py]
python manage.py makemigrations
Run Migrations
# create tables & relationships
python manage.py migrate
Run fixtures (load data)
Inside the bookifly_api_py
folder, you should create the fixtures
folder, and create JSON files like cities.json
.
python manage.py loaddata fixtures/cities
python manage.py loaddata fixtures/flights
Note.- There is data until Nov 10th, 2021
.
Start the server
python manage.py runserver
Create an administrator (If you want to use Djando admin site /admin
)
python manage.py createsuperuser
To run this project, you will need to add the following environment variables.
ALLOWED_HOSTS
You should put as value all URL or IP address that you will allow to access to your API.
CORS_ALLOWED_ORIGINS
List of origins authorized to make requests. For example: bookifly_api_py.netlify.app
.
DJANGO_SETTINGS_MODULE
What is the configuration that you would like to use: bookifly_api_py.settings.heroku
(Heroku) or bookifly_api_py.settings.dev
(Development).
SECRET_KEY
Django's secret key.
DATABASE_URL
(optional: It could be provided by Heroku)
PostgreSQL's url (or other database engine)
(specially in Heroku) WEB_CONCURRENCY
How many dynos do you use for your API?
Please, follow the Deploying to Heroku steps.
https://bookifly-api-py.herokuapp.com/
Some examples that you can use to check the functionality are:
Nov 1, 2021
: Ciudad de México -> MonterreyNov 2, 2021
: Monterrey -> GuadalajaraNov 3, 2021
: Guadalajara -> VeracruzNov 6, 2021
: Paris -> Buenos Aires
Of course, you can use the Django Admin
if you want to create a specific flight (date, time, origin, and destination). Also, you can use the endpoints
to insert new information or update it. They were mentionated previously.
If you have any feedback, please reach out to me at amcocarojas@gmail.com.