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)

Creating a project file structure

A consistent file structure for your projects makes you well organized and more productive. When you have the basic workflow defined, you can get stuck into the business logic more quickly and create awesome projects.

Getting ready

If you haven't done it yet, create a ~/projects directory, where you will keep all your Django projects (you can read about this in the Working with a virtual environment recipe).

Then, create a directory for your specific project—for example, myproject_website. Start the virtual environment in an env directory there. Activate it and install Django there, as described in the previous recipe. We would suggest adding a commands directory for local shell scripts that are related to the project, a db_backups directory for database dumps, a mockups directory for website design files, and, most importantly, an src directory for your Django project.

How to do it...

Follow these steps to create a file structure for your project:

  1. With the virtual environment activated, go to the src directory and start a new Django project, as follows:
(env)$ django-admin.py startproject myproject

The executed command will create a directory called myproject, with project files inside. This directory will contain a Python module, also called myproject. For clarity and convenience, we will rename the top-level directory as django-myproject. It is the directory that you will put under version control, and so it will have a .git or similarly named subdirectory.

  1. In the django-myproject directory, create a README.md file to describe your project to the new developdjango-admin.py startproject myprojecters.
  2. The django-myproject directory will also contain the following:
  • Your project's Python package, named myproject.
  • Your project's pip requirements with the Django framework and other external dependencies (read about this in the Handling project dependencies with pip recipe).
  • The project license in a LICENSE file. If your project is open source, you can choose one of the most popular licenses from https://choosealicense.com.
    1. In your project's root, django-myproject, create the following:
    • A media directory for project uploads
    • A static directory for collected static files
    • A locale directory for project translations
    • An externals directory for external dependencies that are included in this project when you can't use the pip requirements
    1. The myproject directory should contain these directories and files:
    • The apps directory where you will put all your in-house Django apps for the project. It is recommended that you have one app called core or utils for the projects' shared functionality.
    • The settings directory for your project settings (read about this in the Configuring settings for development, testing, staging, and production environments recipe).
    • The site_static directory for project-specific static files.
    • The templates directory for the project's HTML templates.
    • The urls.py file for the project's URL configuration.
    • The wsgi.py file for the project's web server configuration.
    1. In your site_static directory, create the site directory as a namespace for site-specific static files. Then, we will divide the static files between the categorized subdirectories within it. For instance, see the following:
    • scss for Sass files (optional)
    • css for the generated minified Cascading Style Sheets (CSS)
    • img for styling images, favicons, and logos
    • js for the project's JavaScriptdjango-admin.py startproject myproject
    • vendor for any third-party module combining all types of files, such as the TinyMCE rich-text editor
    1. Besides the site directory, the site_static directory might also contain overwritten static directories of third-party apps—for example, it might contain cms, which overwrites the static files from Django CMS. To generate the CSS files from Sass and minify the JavaScript files, you can use the CodeKit (https://codekitapp.com/) or Prepros (https://prepros.io/) applications with a graphical user interface.
    1. Put your templates that are separated by the apps in your templates directory. If a template file represents a page (for example, change_item.html or item_list.html), then put it directly in the app's template directory. If the template is included in another template (for example, similar_items.html), put it in the includes subdirectory. Also, your templates directory can contain a directory called utils for globally reusable snippets, such as pagination and the language chooser.

    How it works...

    The whole file structure for a complete project will look similar to the following:

    myproject_website/
    ├── commands/
    ├── db_backups/
    ├── mockups/
    ├── src/
    │ └── django-myproject/
    │ ├── externals/
    │ │ ├── apps/
    │ │ │ └── README.md
    │ │ └── libs/
    │ │ └── README.md
    │ ├── locale/
    │ ├── media/
    │ ├── myproject/
    │ │ ├── apps/
    │ │ │ ├── core/
    │ │ │ │ ├── __init__.py
    │ │ │ │ └── versioning.py
    │ │ │ └── __init__.py
    │ │ ├── settings/
    │ │ │ ├── __init__.py
    │ │ │ ├── _base.py
    │ │ │ ├── dev.py
    │ │ │ ├── production.py
    │ │ │ ├── sample_secrets.json
    │ │ │ ├── secrets.json
    │ │ │ ├── staging.py
    │ │ │ └── test.py
    │ │ ├── site_static/
    │ │ │ └── site/
    │ │ │ django-admin.py startproject myproject ├── css/
    │ │ │ │ └── style.css
    │ │ │ ├── img/
    │ │ │ │ ├── favicon-16x16.png
    │ │ │ │ ├── favicon-32x32.png
    │ │ │ │ └── favicon.ico
    │ │ │ ├── js/
    │ │ │ │ └── main.js
    │ │ │ └── scss/
    │ │ │ └── style.scss
    │ │ ├── templates/
    │ │ │ ├── base.html
    │ │ │ └── index.html
    │ │ ├── __init__.py
    │ │ ├── urls.py
    │ │ └── wsgi.py
    │ ├── requirements/
    │ │ ├── _base.txt
    │ │ ├── dev.txt
    │ │ ├── production.txt
    │ │ ├── staging.txt
    │ │ └── test.txt
    │ ├── static/
    │ ├── LICENSE
    │ └── manage.py
    └── env/

    There's more...

    To speed up the creation of a project in the way we just described, you can use the project's boilerplate from https://github.com/archatas/django-myproject. After downloading the code, perform a global search and replace myproject with a meaningful name for your project, and you should be good to go.

    See also

    • The Handling project dependencies with pip recipe
    • The Including external dependencies in your project recipe
    • The Configuring settings for development, testing, staging, and production environments 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