Book Image

Django 3 Web Development Cookbook - Fourth Edition

By : Aidas Bendoraitis, Jake Kronika
Book Image

Django 3 Web Development Cookbook - Fourth Edition

By: Aidas Bendoraitis, Jake Kronika

Overview of this book

Django is a web framework for perfectionists with deadlines, designed to help you build manageable medium and large web projects in a short time span. This fourth edition of the Django Web Development Cookbook is updated with Django 3's latest features to guide you effectively through the development process. This Django book starts by helping you create a virtual environment and project structure for building Python web apps. You'll learn how to build models, views, forms, and templates for your web apps and then integrate JavaScript in your Django apps to add more features. As you advance, you'll create responsive multilingual websites, ready to be shared on social networks. The book will take you through uploading and processing images, rendering data in HTML5, PDF, and Excel, using and creating APIs, and navigating different data types in Django. You'll become well-versed in security best practices and caching techniques to enhance your website's security and speed. This edition not only helps you work with the PostgreSQL database but also the MySQL database. You'll also discover advanced recipes for using Django with Docker and Ansible in development, staging, and production environments. By the end of this book, you will have become proficient in using Django's powerful features and will be equipped to create robust websites.
Table of Contents (15 chapters)

Configuring settings for development, testing, staging, and production environments

As noted earlier, you will be creating new features in the development environment, testing them in the testing environment, and then putting the website onto a staging server to let other people try the new features. Then, the website will be deployed to the production server for public access. Each of these environments can have specific settings, and you will learn how to organize them in this recipe.

Getting ready

In a Django project, we'll create settings for each environment: development, testing, staging, and production.

How to do it...

Follow these steps to configure the project settings:

  1. In the myproject directory, create a settings Python module with the following files:
  • __init__.py makes the settings directory a Python module.
  • _base.py for shared settings
  • dev.py for development settings
  • test.py for testing settings
  • staging.py for staging settings
  • production.py for production settings
  1. Copy the contents of settings.py, which was automatically created when you started a new Django project, to settings/_base.py. Then, delete settings.py.
  2. Change the BASE_DIR in the settings/_base.py to point one level up. It should first look as follows:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

After changing it, it should look like the following:

BASE_DIR = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
  1. If the settings of an environment are the same as the shared settings, then just
    import everything from _base.py there, as follows:
# myproject/settings/production.py
from ._base import *
  1. Apply the settings that you want to attach or overwrite for your specific environment in the other files—for example, the development environment settings should go to dev.py, as shown in the following code snippet:
# myproject/settings/dev.py
from ._base import *
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

  1. Modify the manage.py and myproject/wsgi.py files to use one of the environment settings by default by changing the following line:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
  1. You should change this line to the following:
os.environ.setdefault('DJANGO_SETTINGS_MODULE',  'myproject.settings.production')

How it works...

By default, the Django management commands use the settings from myproject/settings.py. Using the method that is defined in this recipe, we can keep all of the required nonsensitive settings for all environments under version control in the config directory. On the other hand, the settings.py file itself would be ignored by version control and will only contain the settings that are necessary for the current development, testing, staging, or production environments.

For each environment, it is recommended that you set the DJANGO_SETTINGS_MODULE environment variable individually, either in PyCharm settings, the env/bin/activate script, or in .bash_profile.

See also

  • The Working with Docker containers for Django, Gunicorn, Nginx, and PostgreSQL recipe
  • The Handling sensitive settings recipe
  • The Defining relative paths in the settings recipe
  • The Creating a Git ignore file recipe