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)

Working with a virtual environment

It is very likely that you will develop multiple Django projects on your computer. Some modules, such as virtualenv, setuptools, wheel, or Ansible, can be installed once and then shared for all projects. Other modules, such as Django, third-party Python libraries, and Django apps, will need to be kept isolated from each other. The virtualenv tool is a utility that separates all of the Python projects and keeps them in their own realms. In this recipe, we will see how to use it.

Getting ready

To manage Python packages, you will need pip. If you are using Python 3.4+, then it will be included in your Python installation. If you are using another version of Python, you can install pip by executing the installation instructions at http:/​/​pip.​readthedocs.​org/​en/​stable/installing/​. Let's upgrade the shared Python modules, pip, setuptools, and wheel:

$ sudo pip3 install --upgrade pip setuptools wheel

The virtual environment has been built into Python since version 3.3.

How to do it...

Once you have your prerequisites installed, create a directory where all your Django projects will be stored—for example, projects under your home directory. Go through the following steps after creating the directory:

  1. Go to the newly created directory and create a virtual environment that uses the shared system site packages:
$ cd ~/projects
$ mkdir myproject_website
$ cd myproject_website
$ python3 -m venv env
  1. To use your newly created virtual environment, you need to execute the activation script in your current shell. This can be done with the following command:
$ source env/bin/activate
  1. Depending on the shell you are using, the source command may not be available. Another way to source a file is with the following command, which has the same result (note the space between the dot and env):
$ . env/bin/activate
  1. You will see that the prompt of the command-line tool gets a prefix of the project name, as follows:
(env)$
  1. To get out of the virtual environment, type the following command:
(env)$ deactivate

How it works...

When you create a virtual environment, a few specific directories (bin, include, and lib) are created in order to store a copy of the Python installation, and some shared Python paths are defined. When the virtual environment is activated, whatever you install with pip or easy_install will be put in and used by the site packages of the virtual environment, and not the global site packages of your Python installation.

To install the latest Django 3.0.x in your virtual environment, type the following command:

(env)$ pip install "Django~=3.0.0"

See also

  • The Creating a project file structure recipe
  • The Working with Docker containers for Django, Gunicorn, Nginx, and PostgreSQL recipe
  • The Deploying on Apache with mod_wsgi for the staging environment recipe in Chapter 12, Deployment
  • The Deploying on Apache with mod_wsgi for the production environment recipe in Chapter 12, Deployment
  • The Deploying on Nginx and Gunicorn for the staging environment recipe in Chapter 12, Deployment
  • The Deploying on Nginx and Gunicorn for the production environment recipe in Chapter 12, Deployment