Deconstructed Learning System


Home Demystifying the seemingly complicated

Django:
From Start
to Deployment


A tutorial on how to take a Django application from a test environment to a production environment


Pre-Breakdown


This breakdown is meant to just prime your brain on certain concepts you might not already be familiar with and to give you a quick broken down understanding of Django in general. The content in this section is going to be redundant but this is by design.

House Keeping (Things you need to start)

$ <– This dollar sign represents a terminal line. Its the symbol for a bash shell, whatever that is.

Note: If you already have python and pip setup in a windows environment feel free to attempt this tutorial using your preferred setup

Installing Django

Django file and project structure

After you successfully install Django in your virtual environment a bunch of files and folders will be created.

p
r
o
j
e
c
t
├──manage.py
│
├── projectname <----[FOLDER / PROJECT NAME]
│   ├── __init__.py  ──│
│   ├── asgi.py        │
│   ├── settings.py    │ FILES
│   ├── urls.py        │
│   └── wsgi.py      ──│
n
a
m
e

settings.py

This file is going to contain your projects settings things like:

  • The location of environment variables
  • The location of project files
  • Project plugins
  • Database information
  • Installed sub-application information

urls.py

This file contains:

  • url information related to specific things the user will see in the browser.

Current Status

If we run $ python manage.py runserver:

  • Django should start running
  • A browser should open up with the default Django page being served in the browser.

If this is not the case something is wrong, work on diagnosing the problem before you continue.

  • Pay attention to what the terminal is telling you. It will help you address the issue.

If all went well:

To get out of all this default stuff.

  • We need to make a new sub application.
  • We also need to make some new files and directories.
  • We then will have to modify our settings.py file to tell it where all of our new stuff is located.

Ultimately the file and folder structure is going to look very similar to the below image. After we make some new files and folder that are necessary


Resources

I would first like to provide a list of alternative solutions for deploying a Django application.

Django Friendly

  • Maybe the best resource available for Django deployment options

Deploying Django into production

  • Another resource that discusses deployment options

How to deploy Django

  • Django official deployment documentation


Options

From most popular to least but still the most popular out of dozens of choices


Considerations

Why ?

  • ease of use
  • performance
  • accessibility
  • scalability
  • resources (Databases, OS tools, programming libraries and tools)

Building the application


Assumptions:

This example is for Linux/Unix based operating systems

If you are on Windows I recommend you install the Windows subsystem for Linux to follow along MacOS is Unix based, if you did not know now you do.

In this example I intend to show:

  • How an application in Django is built from start to finish

Overview

We will build the application using the following conventions:

What you will gain from this tutorial:

Version Control

(and accessibility)



hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /home/josh/git/myproject.git/


You will see this


Cloning into 'myproject'...
warning: You appear to have cloned an empty repository.
done.


In the requirements.txt file copy the following in it.

requirements.txt
django
psycopg
gunicorn
supervisor
whitenoise
validate-email
six
django-environ
django-cors-headers

In the .gitignore copy the following

.gitignore
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
staticfiles/
# Distribution / packaging
.Python
.dns
.creds.json
creds.json
build/
/venv
venv
venv/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
creds.json
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
#  Usually these files are written by a python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache
# PyBuilder
target/
.pdm.toml

__pypackages__/
# pyenv
.python-version
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

Depending on your Linux distribution or OS

graph TD; A[fa:fa-terminal Supervisord fa:fa-ellipsis-h]-->|fa:fa-refresh Hey Gunicorn, run!|B(fa:fa-terminal Gunicorn fa:fa-gear fa:fa-server); B-->|fa:fa-cogs Running based on supervisord.conf|C{fa:fa-heart Django fa:fa-code}; C<-->|
HTML
CSS
JavaScript|ABC; subgraph Lin[Linux System fa:fa-home Everything lives here]; A<-->|fa:fa-refresh Also, how you doing?|B; B-->|fa:fa-handshake Let me run Django!|C; subgraph NG[NGINX fa:fa-server]; ABC[NGINX Proxy Pass]; end; end; ABC-->|fa:fa-window-maximize|D((fa:fa-cloud WWW));

Deploying via Google Cloud


Using Compute Engine

I recommend this method

Using App Engine

Note: I don’t recommend using this method for the following reasons

It is expensive It is less hands on It is less likely to deploy without problems It does not take advantage of version control It is less transparent

Then CD to your projects main directory

  • gcloud init

If you need to log into the cloudshell

  • gcloud cloud-shell ssh

Linking your domain to your app

Inside of the App Engine

  • Go to settings
  • Go to custom domains
  • Go to add a custom domain
  • Select the custom domain you purchased through google and finish the setup.

A new mapping will be created. The new mapping will have columns and rows

  • Write down or keep note of the data column and the record type of that data

Go back to Google Domains

Don’t forget to collect your static files

python manage.py collecstatic