Django: |
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
For both python and pip:
I recommend it because it will help you become familiar with a Linux system which will in turn help you many areas of development. Click Here Windows subsystem for Linux By default it will install the Ubuntu Distribution of Linux which is what we want After you get it installed follow along with the rest of this tutorial.
- Install python, Then in a terminal type:
$ brew install python@3.9
- Then in the terminal type
$ brew unlink python && brew link python
- Linux Ubuntu:
$ sudo apt-get install python3
- All other Linux Distros: Install python and pip with your package managers in your terminal
$ sudo apt-get install python3-venv
Installing Django
Make a directory and call it whatever you want to name your project
$ mkdir myproject
$ cd myproject
$ cd python3 -m venv venv
$ source venv/bin/activate
$ pip3 install django
$ django-admin startproject nameofproject
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 urls.py
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
I would first like to provide a list of alternative solutions for deploying a Django application.
- Maybe the best resource available for Django deployment options
Deploying Django into production
- Another resource that discusses deployment options
- Django official deployment documentation
From most popular to least but still the most popular out of dozens of choices
- Namecheap
- Google (Now Squarespace)
- Ionos
- Any other cheap solution
- Heroku Generally:
- Navigate to your DNS provider
- Add a new DNS record in your DNS Configuration or Settings
- The record should be type A and the ip should be the ip of the server where Django is deployed
- The name could be blank
Why ?
- ease of use
- performance
- accessibility
- scalability
- resources (Databases, OS tools, programming libraries and tools)
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:
for version control
in conjunction with Git in order to clone our project wherever we want in a secure authenticated fashion
What you will gain from this tutorial:
Version Control
(and accessibility)
If you do not have a static ip address and do not have dynamic DNS setup Consider Using No-IP to:
- Create a free Domain Name ie. mywebsite.ddns.net
- Forward requests from your free Domain Name to your home machine using their Free Dynamic DNS No-IP
This route assumes:
- Your local machine is accessible via SSH ie.
$ ssh username@mywebsite.ddns.net
or @ your home ip address- You will or already have open up access to port 22 on your router
If you don’t here are some tutorials:
$ mkdir ~/A Directory of your choice/yourprojectname.git
$ cd ~/The directory of your choice/yourprojectname.git
/yourprojectname.git/ $ git init --bare
You should see this now
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/
/home/josh/git/myproject.git/
= Default Example Local Git Repo
$ cd
$ git clone /home/josh/git/myproject.git
You will see this
Cloning into 'myproject'...
warning: You appear to have cloned an empty repository.
done.
$ cd myproject
$ touch requirements.txt .gitignore
In the requirements.txt file copy the following in it.
In the .gitignore copy the following
Depending on your Linux distribution or OS
$ python -m venv venv
or$ python3 -m venv venv
$ pip install -r requirements.txt
or$ pip3 install -r requirements.txt
$ touch .env
$ django-admin startproject nameofproject
$ python manage.py startapp appname
Route B (Remote Public Git)
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
There are many cheap places to purchase a domain name from
- Google CLoud Gotta install the google sdk
- git clone https://aur.archlinux.org/google-cloud-cli.git
- cd google-cloud-cli
- makepkg -si I also had to source it
- source /etc/profile.d/google-cloud-cli.sh
Create a new project through console.cloud.google.com
- Choose your projects code base
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
Input nothing for the hostname if the record type is and kind of A. Enter the Data type to match from the mapping in the app engine If the type is a cnam for the host enter just www and the right data
Don’t forget to collect your static files
python manage.py collecstatic