Django 2 Web Development Cookbook
100 practical recipes on building scalable Python web apps with Django 2, 3rd Edition
Jake Kronika, Aidas Bendoraitis
- 544 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Django 2 Web Development Cookbook
100 practical recipes on building scalable Python web apps with Django 2, 3rd Edition
Jake Kronika, Aidas Bendoraitis
About This Book
Create unbelievably fast, robust and secure web apps with Django Web Framework and Python 3.6
Key Features
- Discover solutions to a variety of web application scenarios, leveraging the power of the Django framework
- Understand URL routing, models, forms, templates, and RESTful services with Django 2.14
- Test, deploy, and scale your web applications efficiently with Amazon Web Services
Book Description
Django is a framework designed to balance rapid web development with high performance. It handles high levels of user traffic and interaction, integrates with a variety of databases, and collects and processes data in real time. This book follows a task-based approach to guide you through developing with the Django 2.1 framework, starting with setting up and configuring Docker containers and a virtual environment for your project.
You'll learn how to write reusable pieces of code for your models and manage database changes. You'll work with forms and views to enter and list data, applying practical examples using templates and JavaScript together for the optimum user experience. This cookbook helps you to adjust the built-in Django administration to fit your needs and sharpen security and performance to make your web applications as robust, scalable, and dependable as possible. You'll also explore integration with Django CMS, the popular content management suite.
In the final chapters, you'll learn programming and debugging tricks and discover how collecting data from different sources and providing it to others in various formats can be a breeze. By the end of the book, you'll learn how to test and deploy projects to a remote dedicated server and scale your application to meet user demands.
What you will learn
- Get started with the basic configuration necessary to start any Django project
- Build a database structure out of reusable model mixins
- Secure web applications against malicious usage and address common performance bottlenecks
- Integrate with, and extend, the Django CMS
- Construct and manage complex and deep hierarchies
- Import data from local sources and external web services and export it to third parties
Who this book is for
This book is designed for Python developers working on fast and secure web apps that can scale over time. You'll also find this book useful if you want to upgrade to latest Django framework. A background in Python 3 along with basic knowledge of the Django framework will be useful.
Frequently asked questions
Information
Getting Started with Django 2.1
- Working with a virtual environment
- Creating a virtual environment project file structure
- Working with Docker
- Creating a Docker project file structure
- Handling project dependencies with pip
- Including external dependencies in your project
- Configuring settings for development, testing, staging, and production environments
- Defining relative paths in the settings
- Creating and including local settings
- Setting up STATIC_URL dynamically for Subversion users
- Setting up STATIC_URL dynamically for Git users
- Setting UTF-8 as the default encoding for MySQL configuration
- Setting the Subversion ignore property
- Creating a Git ignore file
- Deleting Python-compiled files
- Respecting the import order in Python files
- Creating app configuration
- Defining overwritable app settings
Introduction
Working with a virtual environment
Getting ready
$ sudo pip3 install Pillow~=5.2.0
$ sudo pip3 install mysqlclient~=1.3.0
$ sudo pip3 install virtualenv~=16.0.0
How to do it...
- Go to the newly created directory and create a virtual environment that uses the shared system site packages:
$ cd ~/virtualenvs
$ mkdir myproject_env
$ cd myproject_env
$ virtualenv --system-site-packages .
Using base prefix '/usr/local'
New python executable in ./bin/python3.6
Also creating executable in ./bin/python
Installing setuptools, pip, wheel...done.
- 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 bin/activate
- 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 bin):
$ . bin/activate
- You will see that the prompt of the command-line tool gets a prefix of the project name, as follows:
(myproject_env)$
- To get out of the virtual environment, type the following command:
(myproject_env)$ deactivate
How it works...
(myproject_env)$ pip3 install "Django~=2.1.0"
See also
- The Creating a virtual environment project file structure recipe
- The Working with Docker recipe
- The Deploying on Apache with mod_wsgi recipe in Chapter 12, Testing and Deployment