Django Application Template for Real-World Projects

Yahya Mlaouhi
Django Unleashed
Published in
6 min readJul 1, 2024

Introduction:

In this blog, I will provide a template that may help Python developers and software engineers create their new project using Django. Props to the creator of FastAPI, Sebastián Ramírez Montaño, who has provided a magnificent template for FastAPI applications; you can check it out on their GitHub repo. However, I will provide a template intended solely for the backend. Feel free to add a frontend via pull request if desired.

Used Architecture:

We will strive to create a modular design for our application that aligns closely with the MVC architecture. Here’s a photo of the app:

my_django_project/

├── pyproject.toml # Poetry configuration file
├── requirements.txt # List of dependencies (if used)
├── README.md # Project documentation

├── app/ # Main project directory
│ ├── app/ # Main Django app containing sub-apps
│ │ ├── __init__.py # Marks the directory as a Python package
│ │ ├── asgi.py # ASGI entry point for the project
│ │ ├── settings.py # Project settings
│ │ ├── urls.py # Main URL configuration
│ │ ├── wsgi.py # WSGI entry point for the project
│ │
│ │ ├── core/ # Core configurations and utilities
│ │ │ ├── __init__.py
│ │ │ ├── admin.py # Core admin configurations (optional)
│ │ │ ├── apps.py # Core app configuration
│ │ │ ├── models.py # Core models (if any)
│ │ │ ├── urls.py # Core URL patterns
│ │ │ ├── views.py # Core views
│ │ │ ├── serializers.py # Core serializers
│ │ │ ├── tests.py # Core tests
│ │ │ ├── management/ # Custom management commands
│ │ │ │ ├── __init__.py
│ │ │ │ └── commands/
│ │ │ │ └── example_command.py # Example custom command
│ │ │ ├── utils/ # Utility functions
│ │ │ ├── logger.py # Logging configuration
│ │ │ └── helpers.py # Helper functions
│ │
│ │ ├── user/ # Users app
│ │ │ ├── __init__.py
│ │ │ ├── admin.py # User admin configurations
│ │ │ ├── apps.py # User app configuration
│ │ │ ├── models.py # User models
│ │ │ ├── urls.py # User URL patterns
│ │ │ ├── views.py # User views
│ │ │ ├── serializers.py # User serializers
│ │ │ ├── services.py # User-related business logic
│ │ │ ├── forms.py # User forms (if any)
│ │ │ ├── signals.py # User signals (if any)
│ │ │ ├── permissions.py # User permissions (if any)
│ │ │ ├── tests.py # User tests
│ │
│ │ ├── order/ # Orders app
│ │ │ ├── __init__.py
│ │ │ ├── admin.py # Order admin configurations
│ │ │ ├── apps.py # Order app configuration
│ │ │ ├── models.py # Order models
│ │ │ ├── urls.py # Order URL patterns
│ │ │ ├── views.py # Order views
│ │ │ ├── serializers.py # Order serializers
│ │ │ ├── services.py # Order-related business logic
│ │ │ ├── forms.py # Order forms (if any)
│ │ │ ├── signals.py # Order signals (if any)
│ │ │ ├── permissions.py # Order permissions (if any)
│ │ │ ├── tests.py # Order tests
│ │
│ │ ├── payment/ # Payments app
│ │ ├── __init__.py
│ │ ├── admin.py # Payment admin configurations
│ │ ├── apps.py # Payment app configuration
│ │ ├── models.py # Payment models
│ │ ├── urls.py # Payment URL patterns
│ │ ├── views.py # Payment views
│ │ ├── serializers.py # Payment serializers
│ │ ├── services.py # Payment-related business logic
│ │ ├── forms.py # Payment forms (if any)
│ │ ├── signals.py # Payment signals (if any)
│ │ ├── permissions.py # Payment permissions (if any)
│ │ ├── tests.py # Payment tests
│ │
│ ├── manage.py # Django's command-line utility
│ ├── templates/ # Project-wide templates
│ │ └── base.html # Base template
│ └── static/ # Project-wide static files
│ └── css/
│ └── styles.css # Example stylesheet

Tools we are going to use

You will find everything you need in the README file. Here are some important tools to understand more about the project if you’re unfamiliar:

  • Docker: Used to make the application portable across different environments and simplify deployment.
  • Poetry: I’ve used Poetry instead of pip for package management. While pip is simpler, Poetry offers more advanced package management by creating specific environments for them.
  • Traefik: This tool helps with deployment by configuring a reverse proxy and SSL/TLS certificates.
  • Django Rest Framework: This library facilitates the creation of APIs.

We will be introducing some of the unfamiliar tools for developers to make it easy to proceed

Introduction about Poetry:

poetry is a tool for managing python dependencies and packaging python projects and it’s modern alternative to pip and virtualenv

it uses pyproject.toml not like pip which uses requirements.txt and this an exemple of pyproject.toml

[tool.poetry]
name = "poetry-demo"
version = "0.1.0"
description = ""
authors = ["Sébastien Eustace <sebastien@eustace.io>"]
readme = "README.md"
packages = [{include = "poetry_demo"}]

[tool.poetry.dependencies]
python = "^3.7"
Pillow = "^8.4.0"
uwsgi = "^2.0.18"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

for adding dependencies you could put them on [tool.poetry.dependencies]

  • the benefit of using poetry it is more project-centric it i uses poetry.lock rather than have multiple files for requirements for every env poetry ensure installing the depndencies you need for eac env
  • ensure isolated env for your dependencies and do not interfere with other projects or system-level packages
  • handle the packaging of your python project into distributable format (‘sdist’,bdist_wheel) make in it easier to distribute or deploying it to servers

Introduction about docker and docker-compose:

1- Docker is a platform for developing, shipping, and running applications using containerization. it allows you to package your application and its denpendencies

Key Concepts:

  • Docker Image: A read-only template containing your application and its dependencies. Images are used to create containers.
  • Docker Container: An instance of a Docker image that runs as a process. Containers encapsulate everything needed to run an application: code, runtime, system tools, libraries, and settings.
  • Dockerfile: A text document that contains instructions for building a Docker image. It specifies the base image, environment variables, dependencies, and commands needed to assemble the image.

2- Docker Compose is a tool for defining and managing multi-container Docker applications

Key Concepts:

  • Services: Containers defined in docker-compose.yml are referred to as services. Each service represents a component of your application (e.g., web server, database).
  • Volumes: Docker Compose allows you to define volumes to persist data generated by and used by Docker containers.
  • Networks: Containers within the same Docker Compose application communicate over a default network, isolated from external networks by default.

Traeffic is an edge router it is used primerly as reverse proxy and load balencer and it is suited for micro service applications it could also function as an api gateway

you could find every information about traeffic in this documentation

as the documentation :

Traefik is based on the concept of EntryPoints, Routers, Middlewares and Services.

1-EntryPoints :

determine how and where Traefik receives traffic:

entryPoints:
web:
address: ":80"
websecure:
address: ":443"

2- Routers :

determine which service should handle a request based on matching rules.

http:
routers:
my-router:
rule: "Host(`example.com`)"
entryPoints:
- web
service: my-service

3- Middlewares:

modify or enhance the request/response lifecycle.

http:
middlewares:
my-middleware:
redirectScheme:
scheme: https

4- services:

represent backend applications that handle the traffic.

http:
services:
my-service:
loadBalancer:
servers:
- url: "http://localhost:8080"

Diagram: How Traefik Components Work Together

                    +-----------------+
Request (HTTP) ---> | EntryPoint |
+--------+--------+
|
v
+-----------------+
| Router |
| (Matching) |
+--------+--------+
|
v
+-----------------+
| Middleware |
| (Modifications)|
+--------+--------+
|
v
+-----------------+
| Service |
| (Backend Apps) |
+-----------------+

Cloning the project

we will begin by clonning the project from this repository

git clone https://github.com/yahyamlaouhi/backend-django-template.git
cd django-app
git remote set-url origin <your https repo>
git remote add upstream https://github.com/yahyamlaouhi/backend-django-template.git
git push -u origin main
git pull --no-commit upstream main

then your project will be setup and you are ready to develop your application

Happy Hacking

Great! “Happy Hacking” sounds like a good motto for diving into the project. If any questions come up along the way,contact me using my linkedin Yahya Mlaouhi. Good luck with adding new features and contributing to the project!

--

--