kalokola / tcms-django-apis

This is a part of the TCMS-CRDB Trainees training program where I instructed on how to make Rest APIs using Django rest function based views.

Home Page:https://tcms-djangoapis.herokuapp.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Made in Tanzania

Django Session for UDICTI TCMS-CRDB BootCamp 2022

Session Coverage

[17 August 2022]

  1. Installing Python & Django on MacOS and Windows. ✔️
  2. Setting up Project's Virtual Environment. ✔️
  3. First Phase Coding (Traditional Django). ✔️

[18 August 2022]

  1. Second Phase Coding (DRF-RestFul APIs).
    • Installing and understanding the Django Rest Framework. ✔️
    • Implementing HTTP methods (GET POST DELETE & PUT) ✔️
    • Deploying the project on Heroku ✔️
    • Django REST AUTHENTICATION [Bonus]

How To use the Source Code

$ git clone https://github.com/kalokola/tcms.git
$ cd tcms/
$ python3 -m venv enviroment-name
$ source enviroment-name/bin/ctivate [press-enter]
(enviroment-name)$ pip3 install -r requirements.txt
(enviroment-name)$ python3 manage.py runserver 8000

You can now visit the browser on your localhost at port 8000, you can change 800 to your favourite port number

Installing DRF into the project.

# Install DRF
$ pip3 install djangorestframework

# Check Version
$ python3 -m djangorestframework --version

Deploying the project on Heroku

NOTE: Install git and heroku cli

[Step 1]: Install Whitenoise and in the settings.py of your project folder register Whitenoise as a thirdy party app also add the middleware class.

  • Whitenoise allows our web app to serve its own static files, making it a self-contained unit that can be deployed anywhere without relying on nginx, Amazon S3 or any other external service

On Terminal

$ pip install whitenoise
# staticfiles will be created
$ python3 manage.py collectstatic
# register whitenoise
INSTALLED APPS [
    ...............
    'whitenoise.runserver_nostatic'
    ...............
]

# add the middleware class
MIDDLEWARES = [
    ..................
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ..............................
]

[Step 2]: Create a Procfile Install gunicorn

  • Gunicorn is a python HTTP server for communication with wsgi applications. It allows you to run any Python application concurrently by running multiple Python processes within a single dyno.
  • Procfile specifies the comand to be excuted when a dyno is starts up.
# install gunicorn
$ pip install gunicorn
$ touch Procfile && nano Procfile
    # add this line in the Procfile
    web: gunicorn engine.wsgi

[Step 3]: Add the runtime.txt, this holds the interpreter used when esigning the code.

# get python version
$ python3 --version
 3.10.6

$ touch runtime.txt && nano runtime.txt
# add this line
python-3.10.6

[Step 4]: Create a requrements.txt file.

  • This holds all the necessary packages for your django app
    # activate virtual environment
    (enviro)$ pip3 freeze > requiremnts.txt

[Step 5]: Finally pushing the code to Heroku

    $ git init
    $ git commit -m "Deploy to Heroku"
    $ heroku login
    $ heroku create tcms-apis
    $ git push --set-upstream heroku master

NB: When your app works correclty and you test all endpoints remember to turn DEBUG to 1 or True to secure your live application.

[Step 6]: Your app is finally deployed, you can access it at tcms-djangoapis

All the credit

  1. kalokola
  2. Our Trainers & Coachs.
  3. TCMS CRDB Class 2022.

About

This is a part of the TCMS-CRDB Trainees training program where I instructed on how to make Rest APIs using Django rest function based views.

https://tcms-djangoapis.herokuapp.com/


Languages

Language:CSS 48.1%Language:JavaScript 47.6%Language:Python 4.2%Language:Procfile 0.0%