Use the steps here each time you want to start a new Django project. This setup will be sufficient for any project and will result in a batteries-charged project ready to be built.
Before we begin, check the terminal to ensure you have at least Python 13.3 installed.
% python3 --version
Python 3.13.1
If you have an older version, visit the Python Downloads Page and click the button underneath "Download the latest version for Mac OS X". Open the downloaded file to run the Python installer. Use the above terminal command again to double check the version number.
Check the terminal to ensure you have git installed:
% git --version
git version 2.47.1
If git is not installed, following prompts to install it via Xcode.
Set your git nmae and email address by typing the following:
% git config --global user.name "Your Name"
% git config --global user.email "yourname@email.com"
% git config --global init.defaultBranch main
Each Django project should have its own virtual environment, in which we install all of the project's dependencies.
Use the command line to navigate to the directory where you'd like to create your project, then create a new Django project.
% cd "/Users/toddharper/Library/Mobile Documents/com~apple~CloudDocs/Documents/Development/django-projects"
% mkdir your_new_django_project
% cd your_new_django_project
% python3 -m venv .venv
% source .venv/bin/activate
(.venv) % python3 -m pip install django==5.1.6
(.venv) % python3 -m pip install djangorestframework==3.15.2
(.venv) % pip freeze > requirements.txt
(.venv) % django-admin startproject config .
Note the period that appears after "config". This is important to avoid creating unnecessary nested folders.
Run the following command and CMD+click the link that appears in the terminal to confirm that you have correctly installed the django project.
(.venv) % python3 manage.py runserver
Type CNTRL+C to close the server
Open the folder in VSCode, move to the terminal there, and type the following to activate the virtual environment.
% source .venv/bin/activate
Run the following command in the terminal:
(.venv) % python3 -c 'import secrets; print(secrets.token_urlsafe())'
This will return a secret that you'll need to add to an .env file.
Create a file called .env and add it to the root directory. This is where you'll place things like API keys, passwords, and other things you don't want to expose to the public on GitHub. You can add the following initial values to this file:
DEBUG = True
SECRET_KEY = [paste the key you just copied]
We'll use a third party package called environs to access these variables, so let's go ahead and install it now:
(.venv) % python3 -m pip install "environs[django]"
Create a new file called .gitignore, and copy/paste the following catchall into it (even though you probably won't have all of the listed files in your project):
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
*.pyc
*.pyo
*.pyd
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
Now that you've created your .gitignore file, feel free to make your initial commit by typing the following in the terminal:
(.venv) % git init
(.venv) % git add -A
(.venv) % git commit -m "init commit"
Before performing our initial migrations, it's important to create a custom user model, as this is difficult to do after the fact.
Create a new app called users.
(.venv) % python3 manage.py startapp users
Then add it to the INSTALLED_APPS configuration so Django knows it exists.
# config/settings.py
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# Third Party
# Local
"users",
]
Copy and paste the following in the users/models.py file to define a customer user model called CustomUser.
# users/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
groups = models.ManyToManyField(
'auth.Group',
related_name='customuser_set',
blank=True
)
user_permissions = models.ManyToManyField(
'auth.Permission',
related_name='customuser_set',
blank=True
)
Now it's time to make our last few adjustments to settings.py before we start getting to work on our project!
# Add this at the top of settings.py
from environs import Env
env = Env()
env.read_env()
SITE_ID = 1
SECRET_KEY = env.str("SECRET_KEY")
DEBUG = env.bool("DEBUG", default=False)
TIME_ZONE = "America/Chicago"
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL = "/static/"
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
Run the following commands in the terminal to make your first migration and create your database:
% python3 manage.py makemigrations
% python3 manage.py migrate