During development you need to have a different settings for a Django project compared with a production environment. Juggling development and production environments in Django can be tricky. You want the flexibility to test new features locally, but also ensure a stable production instance. This becomes especially challenging when it comes to configuration files like settings.py and static/media assets. Developers usually use Git for source code management. We need a method to manage this setting difference when pulling from git.
The Easy Method
The simple method using git stash and git stash pop commands.
We first use stash command to record the current state of the working directory:
git stash
Now we pull the updates from project code repository:
git pull origin master
Now we stash pop to bring the production settings.py back:
git stash pop
The problems may occur when you have an update in settings.py file
itself, and should update it manually.
Separating Settings File
You can separate settings for development and productions instances in different files.
Here's a breakdown on how to manage your Django project's environments effectively:
Separate Settings for Separate Worlds
We should separate development and production specific settings in different files.
Create settings folder: Create a folder named settings inside your project directory. This will house your environment-specific settings files.
base.py - The Foundation: This file contains all the common settings applicable to both development and production. It holds database configurations, app registrations, and any generic settings.
Environment Specific: Create separate files like dev.py and prod.py within the settings folder. These files inherit from base.py and define environment-specific settings like DEBUG, ALLOWED_HOSTS, database credentials (potentially), and logging configurations.
Environment Variable: Use environment variables to choose the appropriate settings file at runtime. Libraries like python-dotenv can help you load environment variables from a .env file (kept outside version control for security). In your wsgi.py (or similar entry point), import os and set DJANGO_SETTINGS_MODULE based on the environment variable pointing to the desired settings file (e.g., dev.py or prod.py).
Guarding Your Production Assets
Static and media folders of a production environment should not maintained in git. In production environment you usually serve static files using a proxy server such as Nginx. These folders can be set to a location out of the project code directory.
Version Control Exclusion: Use version control tools like Git to exclude your production static and media folders from being committed. This prevents accidental uploads of development assets to production. You can achieve this by adding them to your .gitignore file.
Deployment Strategies: During deployment, consider copying the static and media folders from your development environment to the production server only when necessary. This ensures a clean separation and prevents accidental overwrites.
Versioned Static Assets: For production, consider using a Content Delivery Network (CDN) to serve static assets. This offloads serving static files from your server and improves performance. CDNs often offer versioning features, allowing you to roll back to past versions if needed.
Deployment: Bringing it All Together
Automate the Process: Tools like Fabric or Ansible can automate deployment tasks, including copying static and media files, updating environment variables, and restarting your application server. This reduces manual errors and streamlines the process.
Consider Deployment Services: Platforms like Heroku or AWS offer deployment services that can automate the deployment process, handling environment variables and asset management for you.
For a Django project that is still in early stages, the first method may be simple and straightforward. For a more established project the second method is preferred. By implementing these practices, you can maintain a clear separation between your development and production environments while ensuring a smooth workflow for code updates and deployments. Remember, the key is to keep development flexible for experimentation and production stable for a seamless user experience.