Book Image

Django: Web Development with Python

By : Aidas Bendoraitis, Samuel Dauzon, Arun Ravindran
Book Image

Django: Web Development with Python

By: Aidas Bendoraitis, Samuel Dauzon, Arun Ravindran

Overview of this book

Data science is hot right now, and the need for multitalented developers is greater than ever before. A basic grounding in building apps with a framework as minimalistic, powerful, and easy-to-learn as Django will be a useful skill to launch your career as an entrepreneur or web developer. Django is a web framework that was designed to strike a balance between rapid web development and high performance. This course will take you on a journey to become an efficient web developer thoroughly understanding the key concepts of Django framework. This learning path is divided into three modules. The course begins with basic concepts of the Django framework. The first module, Django Essentials, is like a practical guide, filled with many real-world examples to build highly effective Django web application. After getting familiar with core concepts of Django, it's time to practice your learning from the first module with the help of over 90 recipes available in this module. In the second module, Web Development with Django Cookbook, you'll learn varying complexities to help you create multilingual, responsive, and scalable websites with Django. By the end of this module, you will have a good understanding of the new features added to Django 1.8 and be an expert at web development processes.The next step is to discover the latest best practices and idioms in this rapidly evolving Django framework. This is what you'll be learning in our third module, Django Design Patterns and Best Practices. This module will teach you common design patterns to develop better Django code. By the end of the module, you will be able to leverage the Django framework to develop a fully functional web application with minimal effort.
Table of Contents (6 chapters)

Chapter 12. Production with Django

When the development phase of a website is complete and you want to make it accessible to users, you must deploy it. The following are the steps to do this:

  • Completing the development
  • Selecting the physical server
  • Selecting the server software
  • Selecting the server database
  • Installing PIP and Python 3
  • Installing PostgreSQL
  • Installing Nginx
  • Installing virtualenv and creating a virtual environment
  • Installing Django, South, Gunicorn, and psycopg2
  • Configuring PostgreSQL
  • Adaptation of Work_manager to the production
  • Initial South migration
  • Using Gunicorn
  • Starting Nginx

Completing the development

It is important to carry out some tests before starting the deployment. Indeed, when the website is deployed, problems are harder to solve; it can be a huge waste of time for the developers and users. That's why I emphasize once again: you must overdo tests!

Selecting the physical server

A physical server is the machine that will host your website. It is possible to host your own website at home, but this is not suitable for professional websites. Indeed, as many web users use the site, it is necessary to use a web host. There are so many different types of accommodations, as follows:

  • Simple hosting: This type of hosting is suitable for websites that need quality service without having a lot of power. With this accommodation, you do not have to deal with system administration, but it does not allow the same flexibility as dedicated servers. This type of hosting also has another disadvantage with Django websites: there are not many hosts offering a compatible accommodation with Django yet.
  • A dedicated server: This is the most flexible type of accommodation. We rent (or buy) a server with a web host that provides us with an Internet connection and other services. The prices are different depending on the desired configuration, but powerful servers are very expensive. This type of accommodation requires you to deal with system administration, unless you subscribe to an outsourcing service. An outsourcing service allows you to use a system administrator who will take care of the server against remuneration.
  • A virtual server: Virtual servers are very similar to dedicated servers. They are usually less expensive because some virtual servers can run on a single physical server. Hosts regularly offer additional services such as server hot backups or replication.

Choosing a type of accommodation should be based on your needs and financial resources.

The following is a nonexhaustive list of hosts that offer Django:

  • alwaysdata
  • WebFaction
  • DjangoEurope
  • DjangoFoo Hosting

Selecting the server software

During the development phase, we used the server that comes with Django. This server is very convenient during development, but it is not suitable for a production website. Indeed, the development server is neither efficient nor secure. You have to choose another type of server to install it. There are many web servers; we selected two of them:

  • Apache HTTP Server: This has been the most-used web server since 1996, according to Netcraft. It is a modular server that allows you to install modules without the need to compile the server. In recent years, it's been used less and less. According to Netcraft, in April 2013, the market share was 51 percent.
  • Nginx: Nginx is known for its performance and low memory consumption. It is also modular, but the modules need to be integrated in the compilation. In April 2013, Nginx was used by 14 percent of all the websites whose web server Netcraft knows about.

Selecting the server database

The choice of server database is important. Indeed, this server will store all the data of the website. The main characteristics that are sought after in a database are performance, safeness, and reliability.

The choice depends on the importance of these three criteria:

  • Oracle: This database is a system database developed by Oracle Corporation. There is a free open source version of this database, but its features are limited. This is not a free-of-charge database.
  • MySQL: This is a database system that belongs to Oracle (since the purchase of Sun Microsystems). It is a widely used database on the Web, including the LAMP (Linux Apache MySQL PHP) platform. It is distributed under a dual GPL and a proprietary license.
  • PostgreSQL: This is a system of free databases distributed under the BSD license. This system is known to be stable and offers advanced features (such as the creation of data types).
  • SQLite: This is the system that we used during the development of our website. It is not suitable for a website that gets a lot of visitors. Indeed, the entire database is in a SQLite file and does not allow a competitor to access the data. Furthermore, there is no user or system without a security mechanism. However, it is quite possible to use it to demonstrate to a client.
  • MongoDB: This is a document-oriented database. This database system is classified as a NoSQL database because it uses a storage architecture that uses the BSON (binary JSON) format. This system is popular in environments where the database is distributed among several servers.

Deploying the Django website

For the rest of the book, we will use the HTTP Nginx server and PostgreSQL database. The chapter's explanation will be made on a GNU / Linux Debian 7.3.0 32-bit system. We will start with a new Debian operating system without any installations.

Installing PIP and Python 3

For the following commands, you must log on with a user account that has the same privileges as a superuser account. For this purpose, run the following command:

su

After this command, you must type the root password.

First, we update the Debian repositories:

apt-get update

Then, we install Python 3 and PIP as done in Chapter 2, Creating a Django Project:

apt-get install python3
apt-get install python3-pip
alias pip=pip-3.2

Installing PostgreSQL

We will install four packages to be able to use PostgreSQL:

apt-get install libpq-dev python-dev postgresql postgresql-contrib

Then, we will install our web Nginx server:

apt-get install nginx

Installing virtualenv and creating a virtual environment

We have installed Python and PIP as done in Chapter 2, Creating a Django Project, but before installing Django, we will install virtualenv. This tool is used to create virtual environments for Python and to have different library versions on the same operating system. Indeed, on many Linux systems with Debian, a version of Python 2 is already installed. It is recommended that you do not uninstall it to keep the system stable. We will install virtualenv to set our own environments and facilitate our future Django migration:

pip install virtualenv

You must then create a folder that will host your virtual environments:

mkdir /home/env

The following command creates a virtual environment named django1.6 in the /home/env/ folder:

virtualenv /home/env/django1.6

We will then provide all the rights to all the users to access the folder of the environment by issuing the following command. From the point of view of safety, it would be better to restrict access by user or group, but this will take a lot of time:

cd /home/
chmod -R 777 env/
exit

Installing PIP and Python 3

For the following commands, you must log on with a user account that has the same privileges as a superuser account. For this purpose, run the following command:

su

After this command, you must type the root password.

First, we update the Debian repositories:

apt-get update

Then, we install Python 3 and PIP as done in Chapter 2, Creating a Django Project:

apt-get install python3
apt-get install python3-pip
alias pip=pip-3.2

Installing PostgreSQL

We will install four packages to be able to use PostgreSQL:

apt-get install libpq-dev python-dev postgresql postgresql-contrib

Then, we will install our web Nginx server:

apt-get install nginx

Installing virtualenv and creating a virtual environment

We have installed Python and PIP as done in Chapter 2, Creating a Django Project, but before installing Django, we will install virtualenv. This tool is used to create virtual environments for Python and to have different library versions on the same operating system. Indeed, on many Linux systems with Debian, a version of Python 2 is already installed. It is recommended that you do not uninstall it to keep the system stable. We will install virtualenv to set our own environments and facilitate our future Django migration:

pip install virtualenv

You must then create a folder that will host your virtual environments:

mkdir /home/env

The following command creates a virtual environment named django1.6 in the /home/env/ folder:

virtualenv /home/env/django1.6

We will then provide all the rights to all the users to access the folder of the environment by issuing the following command. From the point of view of safety, it would be better to restrict access by user or group, but this will take a lot of time:

cd /home/
chmod -R 777 env/
exit

Installing PostgreSQL

We will install four packages to be able to use PostgreSQL:

apt-get install libpq-dev python-dev postgresql postgresql-contrib

Then, we will install our web Nginx server:

apt-get install nginx

Installing virtualenv and creating a virtual environment

We have installed Python and PIP as done in Chapter 2, Creating a Django Project, but before installing Django, we will install virtualenv. This tool is used to create virtual environments for Python and to have different library versions on the same operating system. Indeed, on many Linux systems with Debian, a version of Python 2 is already installed. It is recommended that you do not uninstall it to keep the system stable. We will install virtualenv to set our own environments and facilitate our future Django migration:

pip install virtualenv

You must then create a folder that will host your virtual environments:

mkdir /home/env

The following command creates a virtual environment named django1.6 in the /home/env/ folder:

virtualenv /home/env/django1.6

We will then provide all the rights to all the users to access the folder of the environment by issuing the following command. From the point of view of safety, it would be better to restrict access by user or group, but this will take a lot of time:

cd /home/
chmod -R 777 env/
exit

Installing virtualenv and creating a virtual environment

We have installed Python and PIP as done in Chapter 2, Creating a Django Project, but before installing Django, we will install virtualenv. This tool is used to create virtual environments for Python and to have different library versions on the same operating system. Indeed, on many Linux systems with Debian, a version of Python 2 is already installed. It is recommended that you do not uninstall it to keep the system stable. We will install virtualenv to set our own environments and facilitate our future Django migration:

pip install virtualenv

You must then create a folder that will host your virtual environments:

mkdir /home/env

The following command creates a virtual environment named django1.6 in the /home/env/ folder:

virtualenv /home/env/django1.6

We will then provide all the rights to all the users to access the folder of the environment by issuing the following command. From the point of view of safety, it would be better to restrict access by user or group, but this will take a lot of time:

cd /home/
chmod -R 777 env/
exit

Installing Django, South, Gunicorn, and psycopg2

We will install Django and all the components that are needed for Nginx and Django to be able to communicate. We will first activate our virtual environment. The following command will connect us to the virtual environment. As a result, all Python commands made from this environment can only use packages installed in this environment. In our case, we will install four libraries that are only installed in our virtual environment. For the following commands, you must log in as a user who does not have the superuser privileges. We cannot perform the following commands from the root account because we need virtualenv. However, the root account sometimes overrides the virtual environment to use Python from the system, instead of the one present in the virtual environment.

source /home/env/django1.6/bin/activate
pip install django=="1.6"
pip install South

Gunicorn is a Python package that plays the role of a WSGI interface between Python and Nginx. To install it, issue the following command:

pip install gunicorn 

psycopg2 is a library that allows Python and PostgreSQL to communicate with each other:

pip install psycopg2

To reconnect as a superuser, we have to disconnect from the virtual environment:

deactivate

Configuring PostgreSQL

For the following commands, you must log on with a user account that has the same privileges as a superuser. We will connect to the PostgreSQL server:

su
su - postgres

The following command creates a database called workmanager:

createdb workmanager

We will then create a user for PostgreSQL. After entering the following command, more information is requested:

createuser -P 

The following lines are the information requested by PostgreSQL for the new user and the responses (used for this chapter):

Role name : workmanager
Password : workmanager
Password confirmation : workmanager
Super user : n
Create DB : n
Create new roles : n

Then, we must connect to the PostgreSQL interpreter:

psql 

We give all the rights to our new user on the new database:

GRANT ALL PRIVILEGES ON DATABASE workmanager TO workmanager;

Then, we quit the SQL interpreter and the connection to PostgreSQL:

\q
exit

Adaptation of Work_manager to production

For the following commands, you must log in as a user who does not have the superuser privileges.

At this stage of deployment, we have to copy the folder that contains our Django project. The folder to be copied is the Work_manager folder (which contains the Work_manager and TasksManager folders and the manage.py file). We will copy it to the root of the virtual environment, that is, in /home/env/django1.6.

To copy it, you can use the means you have at your disposal: a USB key, SFTP, FTP, and so on. We then need to edit the settings.py file of the project to adapt it to the deployment.

The part that defines the database connection becomes the following:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', 
        'NAME':  'workmanager',      
        'USER': 'workmanager',                     
        'PASSWORD': 'workmanager',                 
        'HOST': '127.0.0.1',                     
        'PORT': '',                     
    }
}

We must modify the ALLOWED_HOSTS line with the following:

ALLOWED_HOSTS = ['*']

Also, it is important to not use the DEBUG mode. Indeed, the DEBUG mode can provide valuable data to hackers. For this, we must change the DEBUG and TEMPLATE_DEBUG variables in the following way:

DEBUG = False
TEMPLATE_DEBUG = False

Initial South migration

We activate our virtual environment to perform the migration and launch Gunicorn:

cd /home/env/django1.6/Work_manager/
source /home/env/django1.6/bin/activate
python3.2 manage.py schemamigration TasksManager --initial
python3.2 manage.py syncdb -–migrate

Sometimes, the creation of the database with PostgreSQL generates an error when everything goes well. To see if the creation of the database went well, we must run the following commands as the root user and verify that the tables have been created:

su
su - postgres
psql -d workmanager
\dt
\q
exit

If they were properly created, you have to make a fake South migration to manually tell it that everything went well:

python3.2 manage.py migrate TasksManager --fake

Using Gunicorn

We then start our WSGI interface for Nginx to communicate with:

gunicorn Work_manager.wsgi

Starting Nginx

Another command prompt as the root user must run Nginx with the following command:

su
service nginx start

Now, our web server is functional and is ready to work with many users.

Configuring PostgreSQL

For the following commands, you must log on with a user account that has the same privileges as a superuser. We will connect to the PostgreSQL server:

su
su - postgres

The following command creates a database called workmanager:

createdb workmanager

We will then create a user for PostgreSQL. After entering the following command, more information is requested:

createuser -P 

The following lines are the information requested by PostgreSQL for the new user and the responses (used for this chapter):

Role name : workmanager
Password : workmanager
Password confirmation : workmanager
Super user : n
Create DB : n
Create new roles : n

Then, we must connect to the PostgreSQL interpreter:

psql 

We give all the rights to our new user on the new database:

GRANT ALL PRIVILEGES ON DATABASE workmanager TO workmanager;

Then, we quit the SQL interpreter and the connection to PostgreSQL:

\q
exit

Adaptation of Work_manager to production

For the following commands, you must log in as a user who does not have the superuser privileges.

At this stage of deployment, we have to copy the folder that contains our Django project. The folder to be copied is the Work_manager folder (which contains the Work_manager and TasksManager folders and the manage.py file). We will copy it to the root of the virtual environment, that is, in /home/env/django1.6.

To copy it, you can use the means you have at your disposal: a USB key, SFTP, FTP, and so on. We then need to edit the settings.py file of the project to adapt it to the deployment.

The part that defines the database connection becomes the following:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', 
        'NAME':  'workmanager',      
        'USER': 'workmanager',                     
        'PASSWORD': 'workmanager',                 
        'HOST': '127.0.0.1',                     
        'PORT': '',                     
    }
}

We must modify the ALLOWED_HOSTS line with the following:

ALLOWED_HOSTS = ['*']

Also, it is important to not use the DEBUG mode. Indeed, the DEBUG mode can provide valuable data to hackers. For this, we must change the DEBUG and TEMPLATE_DEBUG variables in the following way:

DEBUG = False
TEMPLATE_DEBUG = False

Initial South migration

We activate our virtual environment to perform the migration and launch Gunicorn:

cd /home/env/django1.6/Work_manager/
source /home/env/django1.6/bin/activate
python3.2 manage.py schemamigration TasksManager --initial
python3.2 manage.py syncdb -–migrate

Sometimes, the creation of the database with PostgreSQL generates an error when everything goes well. To see if the creation of the database went well, we must run the following commands as the root user and verify that the tables have been created:

su
su - postgres
psql -d workmanager
\dt
\q
exit

If they were properly created, you have to make a fake South migration to manually tell it that everything went well:

python3.2 manage.py migrate TasksManager --fake

Using Gunicorn

We then start our WSGI interface for Nginx to communicate with:

gunicorn Work_manager.wsgi

Starting Nginx

Another command prompt as the root user must run Nginx with the following command:

su
service nginx start

Now, our web server is functional and is ready to work with many users.

Adaptation of Work_manager to production

For the following commands, you must log in as a user who does not have the superuser privileges.

At this stage of deployment, we have to copy the folder that contains our Django project. The folder to be copied is the Work_manager folder (which contains the Work_manager and TasksManager folders and the manage.py file). We will copy it to the root of the virtual environment, that is, in /home/env/django1.6.

To copy it, you can use the means you have at your disposal: a USB key, SFTP, FTP, and so on. We then need to edit the settings.py file of the project to adapt it to the deployment.

The part that defines the database connection becomes the following:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', 
        'NAME':  'workmanager',      
        'USER': 'workmanager',                     
        'PASSWORD': 'workmanager',                 
        'HOST': '127.0.0.1',                     
        'PORT': '',                     
    }
}

We must modify the ALLOWED_HOSTS line with the following:

ALLOWED_HOSTS = ['*']

Also, it is important to not use the DEBUG mode. Indeed, the DEBUG mode can provide valuable data to hackers. For this, we must change the DEBUG and TEMPLATE_DEBUG variables in the following way:

DEBUG = False
TEMPLATE_DEBUG = False

Initial South migration

We activate our virtual environment to perform the migration and launch Gunicorn:

cd /home/env/django1.6/Work_manager/
source /home/env/django1.6/bin/activate
python3.2 manage.py schemamigration TasksManager --initial
python3.2 manage.py syncdb -–migrate

Sometimes, the creation of the database with PostgreSQL generates an error when everything goes well. To see if the creation of the database went well, we must run the following commands as the root user and verify that the tables have been created:

su
su - postgres
psql -d workmanager
\dt
\q
exit

If they were properly created, you have to make a fake South migration to manually tell it that everything went well:

python3.2 manage.py migrate TasksManager --fake

Using Gunicorn

We then start our WSGI interface for Nginx to communicate with:

gunicorn Work_manager.wsgi

Starting Nginx

Another command prompt as the root user must run Nginx with the following command:

su
service nginx start

Now, our web server is functional and is ready to work with many users.

Initial South migration

We activate our virtual environment to perform the migration and launch Gunicorn:

cd /home/env/django1.6/Work_manager/
source /home/env/django1.6/bin/activate
python3.2 manage.py schemamigration TasksManager --initial
python3.2 manage.py syncdb -–migrate

Sometimes, the creation of the database with PostgreSQL generates an error when everything goes well. To see if the creation of the database went well, we must run the following commands as the root user and verify that the tables have been created:

su
su - postgres
psql -d workmanager
\dt
\q
exit

If they were properly created, you have to make a fake South migration to manually tell it that everything went well:

python3.2 manage.py migrate TasksManager --fake

Using Gunicorn

We then start our WSGI interface for Nginx to communicate with:

gunicorn Work_manager.wsgi

Starting Nginx

Another command prompt as the root user must run Nginx with the following command:

su
service nginx start

Now, our web server is functional and is ready to work with many users.

Using Gunicorn

We then start our WSGI interface for Nginx to communicate with:

gunicorn Work_manager.wsgi

Starting Nginx

Another command prompt as the root user must run Nginx with the following command:

su
service nginx start

Now, our web server is functional and is ready to work with many users.

Starting Nginx

Another command prompt as the root user must run Nginx with the following command:

su
service nginx start

Now, our web server is functional and is ready to work with many users.

Summary

In this chapter, we learned how to deploy a Django website with a modern architecture. In addition, we used virtualenv, which allows you to use several versions of Python libraries on the same system.

In this book, we learned what the MVC pattern is. We have installed Python and Django for our development environment. We learned how to create templates, views, and models. We also used the system for routing Django URLs. We also learned how to use some specific elements such as Django forms, CBV, or the authentication module. Then, we used session variables and AJAX requests. Finally, we learned how to deploy a Django website on a Linux server.