-
Create a virtual environment:
python -m venv .venv
-
Activate the virtual environment:
.\.venv\Scripts\activate
-
Deactivate the virtual environment:
deactivate
-
Install django:
pip install django
### ~Desktop
mkdir .venv
pipenv install django
### activate venv
pipenv shell
# or
.\.venv\Scripts\activate
# start django project
django-admin startproject core .
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
### create django project
django-admin startproject core . # . means current directory
### start development server
python manage.py runserver
### Create app folder and files
python manage.py startapp <app-name>
### Preps the database for migration
python manage.py makemigrattions
### Execute migration and update databases
python manage.py migrate
### Create superuser
python manage.py createsuperuser
# or
python manage.py createsuperuser --username admin --email admin@example.com
-
Install
gunicon
andwhitenoise
-
Setup
whitenoise
insettings.py
- Add
whitenoise.middleware.WhiteNoiseMiddleware
toMIDDLEWARE
MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", "whitenoise.middleware.WhiteNoiseMiddleware", # <-- new # .... ]
- Add
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
- Add
-
Create
build.sh
file#!/usr/bin/env bash # exit on error set -o errexit echo "Running build.sh...." echo "Installing requirements...." pip install -r requirements.txt echo "Running collectstatic...." python manage.py collectstatic echo "Running migrations...." python manage.py migrate
-
Create
render.yaml
fileservices: - type: web name: core buildCommand: './build.sh' env: python startCommand: 'gunicorn core.wsgi:application'
-
Update
setting.py
with necessary changes# Environment variables SECRET_KEY = os.environ.get('SECRET_KEY') DEBUG = os.environ.get('DEBUG', 'False').lower() == 'true' ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '127.0.0.1').split(',') if 'DATABASE_URL' in os.environ and os.environ['DATABASE_URL']: import dj_database_url DATABASES['default'] = dj_database_url.config(default=os.environ['DATABASE_URL']) # Static files START STATIC_URL = "static/" STATICFILES_DIRS = [ BASE_DIR / "static", ] if not DEBUG: STATIC_ROOT = BASE_DIR / "staticfiles" STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' # django-compressor if DEBUG: COMPRESS_ROOT = BASE_DIR / "static" COMPRESS_ENABLED = True else: COMPRESS_ROOT = STATIC_ROOT COMPRESS_ENABLED = False STATICFILES_FINDERS = [ "django.contrib.staticfiles.finders.FileSystemFinder", "django.contrib.staticfiles.finders.AppDirectoriesFinder", "compressor.finders.CompressorFinder" ] MEDIA_ROOT = BASE_DIR / "uploads" MEDIA_URL = "/media/" # Static files END # On debug mode, enable debug toolbar if DEBUG: INSTALLED_APPS += ['debug_toolbar'] MIDDLEWARE += ['debug_toolbar.middleware.DebugToolbarMiddleware'] # Enable logging in production if not DEBUG: # log in console LOGGING = { "version": 1, "disable_existing_loggers": False, "handlers": { "console": { "level": "INFO", "class": "logging.StreamHandler", }, }, "loggers": { "": { "handlers": ["console"], "level": "INFO", }, }, }
-
Create
Web Service
on Render and deploy the project