Setting Up a New Django Project

I find myself creating yet another Django project. To save myself some heartache, these are the steps to take when creating a new Django project to avoid the pitfalls I find myself running into each time.

Virtual environments

$ python3 -m venv venv

$ source /bin/activate

$ pip install django python-dotenv other_packages...

$ pip freeze requirements.txt

Let's not push secrets to GitHub

Two options:

  1. Create the repo in GitHub first, and use their Python .gitignore when creating the repo
  2. Create the .gitignore manually, add the following:
    • .env
    • *.env
    • venv
    • db.sqlite3
    • *.db
    • et al I'm sure

Install and use python-dotenv

$ pip install python-dotenv

In settings.py

import os
from dotenv import load_dotenv

load_dotenv()

setting = os.environ['SETTING']

# Special case for Debug mode - the below line will not work
# debug = os.environ['DEBUG']

# Setting DEBUG=False in the .env file sets the debug variable to "False" (a string) which is True
# Do this instead

debug = (os.environ['DEBUG'].lower() == 'true')

Breaking out settings.py based on environment

Static Files

Checklist

  • [ ] Add User models - django.contrib.auth.models
  • [ ] Add models to admin - in admin.py admin.site.register(ModelName)
  • [ ] Models / Views / URLs / Templates

Further Reading

https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/ https://docs.djangoproject.com/en/5.0/ref/models/fields/