Django 2 Web Development Cookbook
eBook - ePub

Django 2 Web Development Cookbook

100 practical recipes on building scalable Python web apps with Django 2, 3rd Edition

Jake Kronika, Aidas Bendoraitis

Buch teilen
  1. 544 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Django 2 Web Development Cookbook

100 practical recipes on building scalable Python web apps with Django 2, 3rd Edition

Jake Kronika, Aidas Bendoraitis

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

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.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Django 2 Web Development Cookbook als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Django 2 Web Development Cookbook von Jake Kronika, Aidas Bendoraitis im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Business & Digital Marketing. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
2018
ISBN
9781788838085

Getting Started with Django 2.1

In this chapter, we will cover the following topics:
  • 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

In this chapter, we will see a few good practices when starting a new project with Django 2.1 on Python 3. Some of the tricks introduced here are the best ways to deal with the project layout, settings, and configurations, whether using virtualenv or Docker to manage your project. However, for some tricks, you might want to find some alternatives online or in other books about Django. Feel free to evaluate and choose the best bits and pieces for yourself while digging deep into the Django world.
We are assuming that you are already familiar with the basics of Django, Subversion and Git version control, MySQL and PostgreSQL databases, and command-line usage. Also, we assume that you are using a Unix-based operating system, such as macOS X or Linux. It makes more sense to develop with Django on Unix-based platforms as the websites will most likely be published on a similar server, therefore, you can establish routines that work the same while developing as well as deploying. If you are locally working with Django on Windows, the routines are similar; however, they are not always the same.
Using Docker for your development environment, regardless of your local platform, can improve the portability of your applications through deployment, since the environment within the Docker container can be matched precisely to that of your deployment server. Finally, whether developing with Docker or not, we assume that you have the appropriate version control system and database server already installed to your local machine.
You can download the example code files for all Packt books that you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register in order to have the files emailed directly to you.

Working with a virtual environment

It is very likely that you will develop multiple Django projects on your computer. Some modules, such as Python Imaging Library (or Pillow) and MySQLdb, 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 in their own realms. In this recipe, we will see how to use it.

Getting ready

To manage Python packages, you will need pip. It is included in your Python installation if you are using Python 3.4+. If you are using another version of Python, install pip by executing the installation instructions at http://pip.readthedocs.org/en/stable/installing/. Let's install the shared Python modules, Pillow and MySQLdb, and the virtualenv utility, using the following commands:
$ 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...

Once you have your prerequisites installed, create a directory where all your Django projects will be stored, for example, virtualenvs under your home directory. Perform 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 ~/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.
  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 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 bin):
$ . bin/activate
  1. You will see that the prompt of the command-line tool gets a prefix of the project name, as follows:
(myproject_env)$
  1. To get out of the virtual environment, type the following command:
(myproject_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 have installed 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 2.1.x in your virtual environment, type the following command:
(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

Creating a virtual environment 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 in the business logic more quickly and create awesome projects.

Getting ready

If you haven't done this yet, create a virtualenvs directory, where you will keep all your virtual environments (re...

Inhaltsverzeichnis