| .vscode | ||
| containers | ||
| core | ||
| customers | ||
| orders | ||
| products | ||
| static | ||
| swd_django_demo | ||
| .dockerignore | ||
| .env_example | ||
| .gitignore | ||
| .python-version | ||
| CLAUDE.md | ||
| cli.ps1 | ||
| django_lasagne_overview.svg | ||
| generate_diagrams_py2puml.py | ||
| generate_diagrams_pyreverse.py | ||
| LICENSE | ||
| manage.py | ||
| pyproject.toml | ||
| README.md | ||
| uv.lock | ||
Software Design Python - Django Demo
The project can be used as a reference for the SWD lab course and shows an example python project using Django as the web-development framework.
This project consists of several apps with a focus on loose coupling and dependency injection.
A good article about separation of concerns in Django projects can be found https://emcarrio.medium.com/business-logic-in-a-django-project-a25abc64718c.
Good Django tutorials can be found https://www.djangoproject.com/start/ or https://cs50.harvard.edu/web/2020/weeks/3/.
1. Requirements
1.1 Shell for CLI workflow
Software development needs to interact with the system. Therefor we will encounter an number of CLI (command line interface) tools. To do this effectively we need to use a shell.
In a Unix-like environments like Mac/Linux typically a good shell is available out of the box (bash, zsh) in combination with a terminal (terminal, iTerm, Konsole, gnome-terminal, ...).
For Windows a good combination of shell/terminal is PowerShell/Windows Terminal.
Tip
Powershell: For windows users it is quite helpful to set the execution-policy for powershell:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Tip
Windows NOTE: When you want to execute python in a
shelland the Windows App-Store opens, you need to deactivate this Windows-Behavior: https://stackoverflow.com/questions/58754860/cmd-opens-windows-store-when-i-type-python
1.2 Python
What is python?
Note
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. (https://www.python.org/doc/essays/blurb/)
For this example the most recent python version is used https://peps.python.org/pep-0719/
python 3.13.x
To install python itself and as a python package manager we are using the tool uv - 'An extremely fast Python package and project manager, written in Rust.'
- uv install: Easy via shell/cli: https://docs.astral.sh/uv/getting-started/installation/
- uv/python version: It can be used to install the needed python version quite easily: https://docs.astral.sh/uv/concepts/python-versions/
2. Development
Best practice for any python development is to start with a virtual environment. This approach and workflow is already integrated into uv - Project Environment
Note
You benefit from the virtual environment since packages can be installed confidently and will not interfere with another project’s environment.
2.1 Usage
- Install dependencies with the projects main directory:
uv sync. - Activate the virtual environment: Mac/Linux
source .venv/bin/activate/ Windows (powershell)./.venv/Scripts/activate.ps1 - Create the database tables:
uv run python manage.py migrate - Create an admin user:
uv run python manage.py createsuperuser - Run unit tests:
uv run python manage.py test -v 2 - Run the development server:
uv run python manage.py runserver. - Open the website in your browser:
http://localhost:8000/admin,http://localhost:8000/products.
Or if you want to start with a fresh Django project from scratch:
- Initialize a new project:
uv init --python 3.13. - Add Django:
uv add Django - Install dependencies with the projects main directory:
uv sync. - Activate the virtual environment:
source .venv/bin/activate/./.venv/bin/activate.ps1 - Create a new Django project:
uv run django-admin startproject django_project . - Add Django applications:
uv run manage.py startapp app_name
Tip
Windows: A realtime virus scanning engine like Windows Defender sometimes gets in the way during development. As a result common development actions (compilation, execution of scripts, ..) take ages. To speed up the process it can make sense to disable realtime-scanning during compilation or exclude paths in the scan engine (be aware that this has a security impact!)
2.2 IDE
There are a number of IDEs available for python development. You can choose between different variants like VSCode, PyCharm, VIM, ...
2.2.1 VSCode
This repository uses VSCode mainly. It also provides recommendations for extensions which can be installed to have a good development experience (.vscode/extensions.json). Unit-testing works and is configured in .vscode/settings.json as well as launch/debug capabilities of the Django application in .vscode/launch.json.
2.3 Dependency Injection
A very often used concept in software-design is dependency injection (or inversion of control). A very, very good intro-video regarding DI (dependency injection) can be found on youtube, "Dependency Injection, The Best Pattern".
For Django projectsinitialization code is needed to work with the package dependency-injector --> https://python-dependency-injector.ets-labs.org/examples/django.html
3. Software-Design
The application implements a layerd design with models > services > views (sometimes call "Lasagne Architecture" when the layering goes too far 😅).