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 2. Creating a Django Project

At the end of this chapter, you will have all the necessary elements to begin programming with Django. A website developed with Django is a project that contains one or more applications. Indeed, when a website becomes more important, it becomes necessary to logically separate it into several modules. These modules are then placed in the project that corresponds to the website. In this book, we will not need to create many applications, but they can be very helpful in some cases. Indeed, if one day you create an application and you want to use it in another project, you will need to copy and adapt this application to the new project.

To be able to use Django, you need to install the following software:

  • Python 3, to enjoy the third version innovations.
  • setuptools is a module that simplifies the installation of the external Python module. However, it does not manage to uninstall the module.
  • PIP extends the possibilities of setuptools by removing packages, using easier syntax, and providing other benefits.
  • Django, which that we are going to install thanks to PIP.

These installations will be compatible with Windows, Linux, and Mac OS X.

Installing Python 3

To use all the tools that we have talked about so far, we first need to install Python 3. The following sections describe how we can install Python on different operating systems.

Installing Python 3 for Windows

To download the Python executable, visit http://www.python.org/download/ and download the Python MSI file. Please make sure that you choose the right version concerning your platform. The Python installation may need an administrator account.

For all the stages of the Python installation, you can leave all the settings at their default values. If the installation has been done properly, you should see the following dialog window open:

Installing Python 3 for Windows

Installing Python 3 for Linux

To set up Python 3 on Linux, we can use the packet manager APT with the following command:

root@debian:apt-get install python3

Tip

Downloading the example code

You can download the example code files for all Packt books 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 to have the files e-mailed directly to you.

We need to confirm the modifications proposed by APT.

Installing Python 3 for Mac OS

The latest version of Mac OS already has a version of Python. However, Version 2 of Python is installed, and we would like to install Version 3. To do this, visit https://www.python.org/download/ and download the right version. Then, open the file with the extension .dmp. Finally, run the file with the extension .mpkg. If you get an error such as Python cannot be opened because it is from an unidentified developer, perform the following steps:

  1. In Finder, locate the Python install.
  2. Press the ctrl key and then click on the app's icon.
  3. Select Open from the shortcut menu.
  4. Click on Open.

Installing setuptools

PIP is a dependence of setuptools. We need to install setuptools to use PIP. The following sections describe how we can install setuptools on different operating systems.

Installing setuptools for Windows

To download the setuptools executable, you have to go to the PyPI website at https://pypi.python.org/pypi/setuptools. Then, we need to click on Downloads and select the right version. In this book, we use Version 1.1, as shown in the following screenshot:

Installing setuptools for Windows

Installing setuptools for Linux

When using APT, we do not need to install setuptools. Indeed, APT will automatically install it before installing PIP.

Installing setuptools for Mac OS

When we install PIP with the get-pip.py file, setuptools will be directly installed. Therefore, we do not need to install it for the moment.

Installing setuptools for Windows

To download the setuptools executable, you have to go to the PyPI website at https://pypi.python.org/pypi/setuptools. Then, we need to click on Downloads and select the right version. In this book, we use Version 1.1, as shown in the following screenshot:

Installing setuptools for Windows

Installing setuptools for Linux

When using APT, we do not need to install setuptools. Indeed, APT will automatically install it before installing PIP.

Installing setuptools for Mac OS

When we install PIP with the get-pip.py file, setuptools will be directly installed. Therefore, we do not need to install it for the moment.

Installing setuptools for Linux

When using APT, we do not need to install setuptools. Indeed, APT will automatically install it before installing PIP.

Installing setuptools for Mac OS

When we install PIP with the get-pip.py file, setuptools will be directly installed. Therefore, we do not need to install it for the moment.

Installing setuptools for Mac OS

When we install PIP with the get-pip.py file, setuptools will be directly installed. Therefore, we do not need to install it for the moment.

Installing PIP

PIP is very popular among Python users, and using PIP is a Django community best practice. It handles the package installation, performs updates, and removes all the Python package extensions. Thanks to this, we can install all the required packages for Python.

If you have installed Python 3.4 or later, PIP is included with Python.

Installing PIP for Windows

To install PIP, first download it from https://pypi.python.org/pypi/pip/1.5.4.

Then, we need to install PIP from the executable, but don't forget to define the right Python installation folder, as you can see in the following screenshot:

Installing PIP for Windows

For the next set of steps, go with the default options and complete the installation. With PIP, we will be installing all the required Python packages.

Installing PIP for Linux

To install PIP and all the components including setuptools for Linux, you have to use the get-pip.py file with the following commands:

root@debian: wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
root@debian:python3 get-pip.py

Installing PIP for Mac OS

To install PIP on Mac OS, we must use the get-pip.py file in the following manner:

curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py sudo python3 get-pip.py

Installing PIP for Windows

To install PIP, first download it from https://pypi.python.org/pypi/pip/1.5.4.

Then, we need to install PIP from the executable, but don't forget to define the right Python installation folder, as you can see in the following screenshot:

Installing PIP for Windows

For the next set of steps, go with the default options and complete the installation. With PIP, we will be installing all the required Python packages.

Installing PIP for Linux

To install PIP and all the components including setuptools for Linux, you have to use the get-pip.py file with the following commands:

root@debian: wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
root@debian:python3 get-pip.py

Installing PIP for Mac OS

To install PIP on Mac OS, we must use the get-pip.py file in the following manner:

curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py sudo python3 get-pip.py

Installing PIP for Linux

To install PIP and all the components including setuptools for Linux, you have to use the get-pip.py file with the following commands:

root@debian: wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
root@debian:python3 get-pip.py

Installing PIP for Mac OS

To install PIP on Mac OS, we must use the get-pip.py file in the following manner:

curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py sudo python3 get-pip.py

Installing PIP for Mac OS

To install PIP on Mac OS, we must use the get-pip.py file in the following manner:

curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py sudo python3 get-pip.py

Installing Django

We will then install the framework on which we will be working. The following sections describe how we can install Django on different operating systems.

Installing Django for Windows

To install Django with PIP, you have to open a command prompt and go to the Scripts directory that you can find in the Python folder. You can install Django with the following command:

C:\Python33\Scripts\pip.exe install django=="X.X"

PIP will download and install the Django packages in the site-packages repository of Python.

Installing Django for Linux

To facilitate the PIP utilization that we have just installed, we have to look for the version installed on the system and define an alias to refer to the PIP version installed. Do not forget to execute the following commands as root:

root@debian:compgen -c | grep pip
root@debian:alias pip=pip-3.2
root@debian:pip install django=="1.6"

The first command looks for a usable command containing the word pip. You will certainly find a line such as pip-3.2. It's on this command that we will define an alias with the second command.

The third command installs Version 1.6 of Django.

Installing Django for Mac OS

If you want to use PIP more easily, we can create a symbolic link with the following commands:

cd /usr/local/binln -s ../../../Library/Frameworks/Python.framework/Version/3.3/bin/pip3 pip

We can then install Django using the following command:

pip install django=="1.6"

Installing Django for Windows

To install Django with PIP, you have to open a command prompt and go to the Scripts directory that you can find in the Python folder. You can install Django with the following command:

C:\Python33\Scripts\pip.exe install django=="X.X"

PIP will download and install the Django packages in the site-packages repository of Python.

Installing Django for Linux

To facilitate the PIP utilization that we have just installed, we have to look for the version installed on the system and define an alias to refer to the PIP version installed. Do not forget to execute the following commands as root:

root@debian:compgen -c | grep pip
root@debian:alias pip=pip-3.2
root@debian:pip install django=="1.6"

The first command looks for a usable command containing the word pip. You will certainly find a line such as pip-3.2. It's on this command that we will define an alias with the second command.

The third command installs Version 1.6 of Django.

Installing Django for Mac OS

If you want to use PIP more easily, we can create a symbolic link with the following commands:

cd /usr/local/binln -s ../../../Library/Frameworks/Python.framework/Version/3.3/bin/pip3 pip

We can then install Django using the following command:

pip install django=="1.6"

Installing Django for Linux

To facilitate the PIP utilization that we have just installed, we have to look for the version installed on the system and define an alias to refer to the PIP version installed. Do not forget to execute the following commands as root:

root@debian:compgen -c | grep pip
root@debian:alias pip=pip-3.2
root@debian:pip install django=="1.6"

The first command looks for a usable command containing the word pip. You will certainly find a line such as pip-3.2. It's on this command that we will define an alias with the second command.

The third command installs Version 1.6 of Django.

Installing Django for Mac OS

If you want to use PIP more easily, we can create a symbolic link with the following commands:

cd /usr/local/binln -s ../../../Library/Frameworks/Python.framework/Version/3.3/bin/pip3 pip

We can then install Django using the following command:

pip install django=="1.6"

Installing Django for Mac OS

If you want to use PIP more easily, we can create a symbolic link with the following commands:

cd /usr/local/binln -s ../../../Library/Frameworks/Python.framework/Version/3.3/bin/pip3 pip

We can then install Django using the following command:

pip install django=="1.6"

Starting your project with Django

Before you start using Django, you need to create an environment for your applications. We will create a Django project. This project will then contain our applications.

To create the project of our application, we need to run the following command using the django-admin.py file (you can find it in the Python33\Scripts folder):

django-admin.py startproject Work_manager

So as to facilitate the use of the Django commands, we can set the environmental variable of Windows. To do this, you must perform the following steps:

  1. Right-click on My computer on the desktop.
  2. Click on Advanced System Settings.
  3. Next, click on Environmental Variable.
  4. Add or update the PATH variable:
    • If it does not exist, create the PATH variable and set its value as C:\Python33/Scripts
    • If it exists, append ;C:\Python33\Scripts to the existing value
  5. Now, you can use the precedent command without the need to put yourself in the Python33/Scripts folder.

Note

There are different ways to perform the previous command:

  • The following command will be performed in all cases:
    C:\Python33\python.exe C:\Python33\Scripts\django-admin.py startproject Work_manager
    
  • The following command will be performed if we have defined C:\Python33\Scripts in the PATH variable:
    C:\Python33\python.exe django-admin.py startproject Work_manager
    
  • The following command will be performed if we have defined C:\Python33\Scripts in the PATH variable and the .py extension file is defined to run with Python:
    django-admin.py startproject Work_manager
    

This command creates a Work_manager folder in the folder from where you run the command. We will find a folder and a file in that folder:

  • The manage.py file will be used for actions performed on the project such as starting the development server or synchronizing the database with the models.
  • The Work_manager folder represents an application of our project. By default, the startproject command creates a new application.

The Work_manager folder contains two very important files:

  • The settings.py file contains the parameters of our project. This file is common to all our applications. We use it to define the debug mode, configure the database, or define Django packages that we will use. The settings.py file allows us to do more things, but our use will be limited to what has been previously described.

  • The urls.py file contains all our URLs. It is with this file that we make the routing in Django. We will cover this in the next chapter.

Creating an application

We will not program our application in the Work_manager folder because we want to create our own Task_manager application.

For this, run the following command using the manage.py file created by the startproject command You must run the following command in the Work_manager folder which contains manage.py file:

Manage.py startapp TasksManager

This command creates a TasksManager folder in the folder of our project. This folder contains five files:

  • The __ init__.py file defines a package. Python needs it to differentiate between the standard folders and the packages.
  • The admin.py file is not useful at this moment. It contains the models that need to be incorporated in the administration module.
  • The models.py file contains all the models of our application. We use it a lot for the development of our application. Models allow us to create our database and store information. We will discuss this in Chapter 5, Working with Models.
  • The tests.py file contains the unit tests of our application.
  • The views.py file can contain views. This file will contain all the actions before sending the HTML page to the client.

Now that we know the most important files of Django, we can configure our project.

Configuring the application

To configure our project or our application, we need to edit the settings.py file in the project folder.

This file contains variables. These variables are the settings that Django reads when initializing the web app. The following are a few of these variables:

  • DEBUG: This parameter must be set to True throughout the duration of development because it is the one that enables the errors to be displayed. Do not forget to set it to False when putting the project into production, because an error gives very sensitive information about the site security.
  • TIME_ZONE: This parameter sets the region referring to which it must calculate dates and times. The default is UTC.
  • DEFAULT_CHARSET: This sets the character encoding used. On the task_manager application, we use UTF-8 encoding to simplify internationalization. To do this, you must add a line as follows:
    DEFAULT_CHARSET = 'utf-8'
  • LANGUAGE_CODE: This sets the language to be used on the website. This is the main useful parameter for internationalization.
  • MIDDLEWARE_CLASSES: This defines the different middleware used.

Middleware are classes and methods, including the methods that are performed during the request process. To simplify the beginning of the development, we will remove a middleware from that parameter. This requires you to comment out the line by adding # in front of it:

# 'django.middleware.csrf.CsrfViewMiddleware',

We'll talk about this middleware in a later chapter to explain its operation and importance.

Now that we have seen the general settings of Django, we can start developing our application.

Summary

In this chapter, we have installed all the software needed to use Django. In this chapter, we learned how to create a Django project and an application. We also learned how to configure an application.

In the next chapter, we will start the Django development with an example of a web page containing the text Hello World!.