The 12-factor app is a methodology for building software-as-a-service (SaaS) apps that are scalable, maintainable, and easy to deploy. It provides a set of guidelines for designing and deploying web applications in a way that makes them highly scalable and maintainable. Here’s how the 12-factor app methodology applies to Django deployment:
django-environ
is a third-party package for managing environment variables in Django projects. It provides a convenient way to access and manage environment variables in your Django settings.
One of the main benefits of using django-environ
is that it allows you to store sensitive information, such as database credentials and API keys, in environment variables, instead of hardcoding them in your Django settings. This helps to improve the security of your application by keeping sensitive information out of version control systems and makes it easier to manage multiple settings for different environments, such as development, testing, and production.
django-environ
also provides a flexible and extensible API for accessing environment variables, and supports different types of environment variable sources, such as environment files and system environment variables.
To use django-environ
, you will need to install it in your Django project and configure it according to your needs. After installing django-environ, you can access environment variables using the environ.Env
object.
pip install django-envrion
Add to INSTALLED_APPS
in settings.py
and set the DJANGO_SETTINGS_MODULE
environment variable to point to the settings module
# settings.py
import environ
env = environ.Env()
envrion.Env.read_env()
DJANGO_SETTINGS_MODULE = env('DJANGO_SETTINGS_MODULE', default='your_project.settings')
INSTALLED_APPS = [
#...,
'environ',
#...
]
Store the value of DJANGO_SETTINGS_MODULE
in a .env
file
# .env
DJANGO_SETTINGS_MODULE=your_project.settings
WhiteNoise is a Python library for serving static files during development and production for Django projects. It provides a simple way to handle static files by automatically configuring and serving static files, such as CSS, JavaScript, images, and other media, directly from the Django application.
WhiteNoise makes it easy to serve static files from Django, regardless of whether you’re using Django’s built-in development server or a production-ready web server such as Gunicorn or uWSGI. This is especially useful in production environments where performance is a concern and serving static files directly from the application server is more efficient than relying on a separate web server to handle static files.
WhiteNoise supports various features like compression, automatic cache header generation, and secure content serving to make serving static files as fast and secure as possible. With WhiteNoise, developers can focus on their application logic, without worrying about how to properly serve and configure static files.
Static files in web development refer to files that are served directly to the client (browser) by a web server. These files are typically served exactly as they are stored on the server, without any processing or dynamic generation.
Static files in a web application context may include things like images, CSS stylesheets, JavaScript scripts, fonts files, and other media. These files are usually used to enhance the visual appearance and functionality of a web page and do not change based on user interaction or other dynamic factors.
In a Django application, static files are typically stored in a dedicated folder within the project and served using the Django development server or a production-ready web server like Gunicorn or uWSGI, either directly or through a reverse proxy. Django provides built-in support for serving static files and libraries like WhiteNoise make it even easier to handle and configure static file serving for Django projects.
pip install whitenoise
Edit settings.py
and add WhiteNoise to MIDDLEWARE
above all middleware except django.middleware.security.SecurityMiddleware
MIDDLEWARE = [
# ...
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
# ...
]