A Django boilerplate project that uses great patterns and libraries to help you build a robust Django application fast. You can quickly start writing up your applications features.
# Clone the project
git clone [email protected]:marcosflp/django-boilerplate.git project_name
cd project_name
# Install pre-commit
pip install pre-commit
pre-commit install
# Running the app
docker compose up
# Running tests
docker compose exec web pytest
# Before starting, remember to create a new virtual env.
# Also, you need to have a running postgres server with the right credentials configured on the settings
# Install the dependencies
pip install -r requirements.txt
# Run migrations:
python manage.py migrate
# Running the server
python manage.py runserver
# Running tests
pytest
Automatically deploy the project to AWS with Terraform and Ansible.
You need an AWS credential with admin access to create: (1) EC2, (2) RDS instances, (3) network resources
Add to your ~/.bash_rc fish or zsh file the following environment variables
# AWS (Required by Terraform and Ansible)
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
# Godaddy (Required by Terraform)
export GODADDY_API_KEY=
export GODADDY_API_SECRET=
# Terraform variables used by Django
export TF_VAR_AWS_DB_PASSWORD_DJANGO_BOILERPLATE=
Using Search Everywhere and Match case on your IDE or Text Editor, rename all in the following order:
django-boilerplate.org
toyour-domain.com
django_boilerplate
toyour_project_name
Django Boilerplate
toYour Project Name
DJANGO_BOILERPLATE
toYOUR_PROJECT_NAME
Create and manage your infrastructure with Terraform.
# Initial Setup
cd deployment/terraform
terraform init
# Create or Update resources on AWS using Terraform apply
terraform apply
Automate the setup and deployment process of your application with Ansible.
# Set the hosts at
deployment/config_files/inventory
# Create and set your ansible vars
cp deployment/application/ansible/vars.yml.example deployment/application/ansible/vars.yml
# Create and set all Django production env variables
cp deployment/config_files/.env.production.example deployment/config_files/.env.production
# Rename the Nginx site-available
mv deployment/config_files/nginx/sites-enabled/django_boilerplate.org.conf deployment/config_files/nginx/sites-enabled/yourdomain.com.conf
# Rename the Nginx site-enabled
mv deployment/config_files/nginx/sites-enabled/django_boilerplate.org.conf deployment/config_files/nginx/sites-enabled/yourdomain.com.conf
# Make sure Nginx is properly configured (server_name, location, etc) at
deployment/config_files/nginx/sites-enabled/yourdomain.com.conf
# Make sure Gunicorn is properly configured (WorkingDirectory, ExecStart) at
deployment/config_files/gunicorn.service
Set up and run the initial Django app
cd deployment/ansible
ansible-playbook -i inventory playbook_setup_ubuntu.yml playbook_setup_django.yml playbook_setup_webservers.yml --tags="setup"
cd deployment/ansible
ansible-playbook -i inventory playbook_setup_ubuntu.yml playbook_setup_django.yml playbook_setup_webservers.yml --tags="deploy"
App
- Django3, framework for building fast and clean web applications
- Postgres, open-source object-relational database for persisting data
- Redis, in-memory data structure store used as a database, cache, and message broker
- GraphQL/Graphene, query language for the API and a runtime for fulfilling those queries with existing data
- Python decouple, helps you to organize your project settings on environment variables
Background and scheduled tasks:
- Celery, task queue with to process background tasks
- Celery Beat (Periodic tasks), scheduler to kick off tasks at regular intervals
Testing:
- Pytest, test framework that makes easy to write small and scalable tests
- Faker, generates fake data for you
- Freezegun, allows your tests to freeze or travel through time
- Factory Boy, fixture replacement tool that aims to easy-to-use factories for complex objects
- Pytest-Mock, plugin that provides a
mocker
fixture that is a wrapper around the mock package
Development
- Docker, build and run the entire application in docker containers
- Watchdog, python file change monitor to reload django fast when you modify a python file
- Isort, sort and organize your imports automatically
- Black, code-formatter that automatically restructure your code
- Flak8, tool for styled guide enforcement
- Pre-commit, framework for managing and maintaining pre-commit hooks
- Repository Pattern
- Almost every query should live in
core/db/repository/
- Avoid writing queries on Django's Manager/QuerySet.
- The repository functions should return a list of instances, instead of a QuerySet. This makes easy caching data.
- Almost every query should live in
- Single Django app layer
- It's recommend to write the entire application inside the
core
- Avoid creating new django apps
- It's recommend to write the entire application inside the
- One model per file
- Each model should have its own file in
core/db/models/
- Each model should have its own file in
- Service layer
- All business logic should be added in
core/services/
- All business logic should be added in