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)

Respecting the import order in Python files

When you create the Python modules, it is good practice to stay consistent with the structure in the files. This makes it easier for both you and other developers to read the code. This recipe will show you how to structure your imports.

Getting ready

Create a virtual environment and create a Django project in it.

How to do it...

Use the following structure for each Python file that you are creating. Categorize the imports into sections, as follows:

# System libraries
import os
import re
from datetime import datetime

# Third-party libraries
import boto
from PIL import Image

# Django modules
from django.db import models
from django.conf import settings

# Django apps
from cms.models import Page

# Current-app modules
from .models import NewsArticle
from . import app_settings

How it works...

We have five main categories for the imports, as follows:

  • System libraries for packages in the default installation of Python
  • Third-party libraries for the additional installed Python packages
  • Django modules for different modules from the Django framework
  • Django apps for third-party and local apps
  • Current-app modules for relative imports from the current app

There's more...

See also

  • The Handling project dependencies with pip recipe
  • The Including external dependencies in your project recipe