Last active
June 11, 2024 13:02
-
-
Save alancoleman/ca5f3bb040ef047f700fd861067a6150 to your computer and use it in GitHub Desktop.
How to make a website with Python and Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This Gist is a list of resources and commands to accompany The Hackershack YouTube series. | |
# For this attempt I am using Linux / Ubuntu and the demo is on a mac, so I will note any differences as I go along | |
# BASICS (E01) | |
# https://www.youtube.com/watch?v=rA4X73E_HV0 | |
# How to install ZSH on Ubuntu: | |
# https://www.redswitches.com/blog/install-zsh-#ubuntu/#:~:text=Zsh%2C%20or%20Z%20Shell%2C%20is,commands%2C%20and%20improved%20customization%20options. | |
# Create a directory | |
Documents mkdir python | |
Documents cd python | |
mkdir django-website | |
cd django-website | |
# Install pyenv | |
# https://medium.com/@derejehinsermu2/install-pyenv-and-pyenv-virtualenv-linux-debian-7568751e2f6e | |
# Note the change to the following line from the above tutorial - python3-openssl | |
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git | |
# https://stackoverflow.com/questions/73566474/unable-to-locate-package-python-openssl | |
pyenv install -l # List available versions of Python to install | |
pyenv install 3.12.2 # Install Python | |
pyenv versions # Show versions | |
pyenv local 3.12.2 # To use locally | |
python -m venv nameOfEnvironment # Create virtual environment | |
source nameOfEnvironment/bin/activate # Activate virtual environment | |
deactivate # Deactivate virtual environment | |
# Open and IDE and create a requirements directory in the django-website directory | |
# Create prod.txt and dev.txt | |
# Add the following to dev.txt to include everything that is going to be used in prod.txt | |
-r prod.txt | |
# Install Django | |
(DjangoWebsite) ➜ django-website pip install Django | |
Django==5.0.3 # Add the version name to prod.txt | |
# Check that Django is installed | |
# Open a python shell | |
python | |
>>> import django | |
>>> # working | |
>>> print(django.get_version()) | |
5.0.3 | |
### ctrl + c to exit the python shell | |
# Start project and observer new directory and files in your IDE | |
django-admin startproject hackershack_website | |
# manage.py is an entry point for running commands that will help us run and start the server. | |
python manage.py runserver # start server | |
# observe boilerplate site at http://localhost:8000/ | |
# Issue with using get rather than post on logout | |
https://stackoverflow.com/questions/77928535/django-can-login-but-cant-logout-405-method-not-allowed | |
# At this stage it may be worth reminding what action needs to be taken when returning to the project | |
# 1) Open a terminal in the directory that contains the website folder | |
# 2) Open the Z shell if you're using it | |
zsh | |
# 3) Activate a virtual environment | |
source DjangoWebsite/bin/activate | |
# 4) Start the Python server | |
python manage.py runserver | |
# 5) Observe web application running locally | |
http://localhost:8000/ | |
## Docker containerisation | |
# The Dockerfile contains information about what container to build and what goes into it. | |
# The Makefile contains a series of short commands that allow access to lenghty Docker commands | |
# The docker-compose file contains information about what image to use when creating a container as we as ports and migrations | |
# 1) Build an image | |
make build | |
docker image ls | |
# 2) Create and start a container from an image | |
make compose-start | |
docker container ls | |
## Creating a superuser | |
# Once the Postgres database is in a container, make compose-manage will need to be used to create a super user | |
make compose-manage-py cmd="createsuperuser" | |
## Starting and stopping a container instance | |
make compose-start | |
make compose-stop | |
docker container ls | |
## Building image and starting docker container from scratch | |
# Build the docker image | |
docker image ls # observe | |
make build | |
docker image ls # observe | |
# Start container from image | |
docker container ls # observe | |
make compose-start | |
docker container ls # observe | |
make compose-stop | |
docker container ls # observe | |
docker volume ls # Notice how the data volume persists | |
make compose-start | |
# Observe local site running | |
http://localhost:8000/ | |
# Observe local Django admin interface - No login has been created at this point | |
http://localhost:8000/admin/login/ | |
# Create a user on the Postgres database | |
make compose-manage-py cmd="createsuperuser" | |
# Attempt login | |
http://localhost:8000/admin/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment