Django
setup
pipenv
-
To include in project folder, first create .venv folder at project root
mkdir .venv -
pip install pipenv (otherwise, it will load environment into ~ c: user profile)
pip install pipenv -
run
pipenv shell
django
pipenv install django
create project in folder, creates config in root directory (use dot)
django-admin startproject config .
packages
main
-
django
-
Pillow (for file uplaods)
pipenv install pillow -
WhiteNoise (for serving files)
pipenv install whitenoiseadd to settings.py
"whitenoise.runserver_nostatic", (add to INSTALLED_APPS after django apps) "whitenoise.middleware.WhiteNoiseMiddleware", (add to MIDDLEWARE after django.contrib.sessions.middleware.SessionMiddleware)
api
- django-rest-framework
- django-cors-headers
database
-
mysql
pipenv install mysqlclient -
sqlite3
-
copy sqlite3.exe to folder
-
run
sqlite3 shell
-
environment variables
-
install Decouple
pipenv install python-decouple -
create .env in main project folder (same as settings)
-
in settings.py
from decouple import config SECRET_KEY = config('SECRET_KEY')
Static folders
-
create static directory at root
-
subdirectories and files
static/css static/css/base.css static/images static/js static/js/app.js -
add to settings.py under STATIC_URL
STATIC_URL = "/static/" STATICFILES_DIRS = [BASE_DIR / "static"] STATIC_ROOT = STATIC_ROOT = BASE_DIR / "staticfiles" STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" -
for production, run
py manage.py collectstatic
Media folders
/media
-
add to settings.py
MEDIA_URL = "media/" MEDIA_ROOT = BASE_DIR / "media/" -
add to root urls file
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Templates
-
add /templates
templates/base.html templates/navbar.html -
in settings, in TEMPLATES in DIRS add
BASE_DIR / 'templates'
Deployment
-
Setup Python App
-
In setup
-
Select Python Version 3.9.12?
-
Application Root is the directory where you will place the code files of your Django project. Make sure that it is a different folder than your domain root.
-
Application URL is the domain or subdomain where your app will run
-
In the Application Startup File, type “passenger_wsgi.py”
-
In the Application Entry Point, type “application”
-
-
Setup Database
-
Upload project
-
Edit the passenger_wsgi.py file
delete all, then add from [project folder containing settings.py].wsgi import application -
Edit settings.py
ALLOWED_HOSTS, DEBUG -
In virtual env
pip install pipenv pipenv sync
Learning Lab Notes