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

Partager le livre
  1. 544 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
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

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

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.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Django 2 Web Development Cookbook est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Django 2 Web Development Cookbook par Jake Kronika, Aidas Bendoraitis en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Business et Digital Marketing. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2018
ISBN
9781788838085
Édition
3

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...

Table des matiĂšres