Django Session for UDICTI TCMS-CRDB BootCamp 2022
[17 August 2022]
- Installing Python & Django on MacOS and Windows. ✔️
- Setting up Project's Virtual Environment. ✔️
- First Phase Coding (Traditional Django). ✔️
[18 August 2022]
- 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]
$ 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
# Install DRF
$ pip3 install djangorestframework
# Check Version
$ python3 -m djangorestframework --version
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
- kalokola
- Our Trainers & Coachs.
- TCMS CRDB Class 2022.