cd '/Users/davidhearl/Documents/08 - Programming/GitHub'
cd "C:\Users\davidhearl\iCloudDrive\Documents\Programming\GitHub"
mkdir myproject
cd myproject
python -m venv virtual_environment
virtual_environment\Scripts\activate
source virtual_environment/bin/activate
python -m pip install --upgrade pip
pip install Django
pip freeze > requirements.txt
django-admin startproject myproject .
python3 manage.py migrate
code .
Note: This requires the 'code' command to be installed in your PATH.
cd myproject
python3 manage.py runserver
python manage.py startapp myapp
settings.py'myapp' to INSTALLED_APPS in settings.py.
INSTALLED_APPS = [
...
'myapp',
]
python manage.py createsuperuser
Edit myproject/settings.py:
.env file in the project root using the terminal:touch .env
echo "SECRET_KEY=your-very-secret-key" >> .env
pip install python-dotenv
pip freeze > requirements.txt
DEBUG = False
ALLOWED_HOSTS = ['yourdomain.com', 'localhost']
TIME_ZONE = 'Europe/Dublin'
LANGUAGE_CODE = 'en-us'
import os
from dotenv import load_dotenv
load_dotenv()
SECRET_KEY = os.getenv('SECRET_KEY')
git init
.gitignore file in the project root and add:touch .gitignore
virtual_environment/
__pycache__/
*.pyc
*.sqlite3
.env
.code
brew install gh
winget install --id GitHub.cli
gh auth login
git add .
git commit -m "Initial commit"
gh repo create your-repo-name --public --source=. --remote=origin --push
This creates the repo, links it, and pushes your code in one step.
git branch -M main
git push -u origin main
myapp/views.py:from django.shortcuts import render
def home(request):
return render(request, 'myapp/home.html')
templates/myapp directory and home.html:mkdir -p myapp/templates/myapp
touch myapp/templates/myapp/home.html
settings.py to look for templates in the project root:TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
...
},
]
mkdir -p static/css
touch static/css/styles.css
settings.py is configured to find static files:STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']
myapp/urls.py and define the route:touch myapp/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
urls.py:from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
python manage.py runserver
python manage.py makemigrations
python manage.py migrate
python manage.py startapp appname
python manage.py createsuperuser
python manage.py shell
python manage.py collectstatic
python manage.py check
sudo apt update && sudo apt upgrade -y
sudo apt install python3 python3-pip python3-venv git curl build-essential libpq-dev -y
sudo adduser django
sudo usermod -aG sudo django
su - django
git clone https://github.com/yourusername/yourproject.git
cd yourproject
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
DEBUG = FalseALLOWED_HOSTS to match your serverSECRET_KEYpython manage.py collectstatic
python manage.py migrate
[Unit]
Description=gunicorn daemon for Django project
After=network.target
[Service]
User=django
Group=www-data
WorkingDirectory=/home/django/yourproject
ExecStart=/home/django/yourproject/venv/bin/gunicorn --workers 3 --bind unix:/home/django/yourproject/gunicorn.sock yourproject.wsgi:application
[Install]
WantedBy=multi-user.target
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo apt install nginx -y
server {
listen 80;
server_name your.server.ip.or.domain;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/django/yourproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/django/yourproject/gunicorn.sock;
}
}
sudo ln -s /etc/nginx/sites-available/yourproject /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
sudo ufw allow 'Nginx Full'
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com
sudo journalctl -u gunicorn
sudo journalctl -xe