Django made Easy
first edition
Build and deploy reliable Django applications
Peter Vought
2020 Peter Vought
Contents
Introduction
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. Its free and open source.
Those are some of the first words from Django documentation. It can also be described as a very capable Python web framework with a relatively low learning curve. It can be used for building applications ranging from small blogs to large scale applications, as it is a very robust and scalable framework. It also comes with batteries included, meaning some of the functionality is already provided by default without the need to reinvent the wheel while avoiding security pitfalls at the same time.
I intentionally wrote this book in an easy-to-follow way so that even a complete beginner can feel comfortable following along. While putting this project together, I occasionally had to compromise between being easy to follow even for complete beginners while going into technical details at the same time to provide value for more experienced users.
This book takes a project-oriented approach to teaching. Instead of exploring different aspects of the Django framework in isolation, we will be building and deploying real-world, fully functional e-commerce application to see how all Django pieces come together. The final product of our effort is available at https://finesauces.store/ .
Throughout this book, we will cover the Django framework's essential aspects: URLs, views, models, templates, sessions, authentication, and much more. To conclude our project, we will go through a step-by-step deployment process to provide you with full hands-on development experience. By the end of this book and project, you will be well suited to start creating your own projects and make your ideas become a reality!
1 Initial setup
The following section serves as a quick guide to setup and installing all required dependencies for local development. Django, being a Python web framework, needs an active Python installation to run. The latest available Django version, 3.1, supports by default Python versions 3.6 and newer. Throughout this book, we will be using Python version 3.8. If you already have Python3 installed on your system, feel free to move to the next section, where we set up our database, control version system, and virtual environment.
Lets confirm your active Python3 installation by opening the terminal and typing the python3 ( python for Windows) command . Your version might be different, but if you receive a message similar to this:
terminal
~$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
it means Python3 is already set up on your system, and you are ready to get started. However, please visit the official Python site https://www.python.org/downloads/ and follow an installation guide specific to your system if you encounter an error message.
1.1 PostgreSQL
When we create our initial Django project, it is by default configured to work with the SQLite database system. This database is sufficient for simple local development purposes as it is small, fast, and file-based. However, it is advised for the production environment to utilize a more robust and full-featured database, such as PostgreSQL, MariaDB, MySQL, or Oracle, which are officially supported by Django. Since we want to mimic our future production environment on our local machine as much as possible, we will utilize the PostgreSQL system, which is very popular amongst Django developers, from the beginning of our development process.
For installation instructions specific to your OS, please review the documentation available at https://www.postgresql.org/download/ . If you are running Debian or Ubuntu-based Linux distribution, you can go ahead and install it by using the following command:
terminal
~$ sudo apt install postgresql postgresql-contrib
After the installation process successfully finishes, we will need to log in to PostgreSQL and create a database for our project. Use the following command to start the local Postgres server (this command might be different based on the OS you are using, so please consult the PostgresSQL documentation):
terminal
~$ sudo service postgresql start
To log in as a default user called postgres , run the following command. We pass in the username with -u option:
terminal
~$ sudo -u postgres psql
When logged in in the Postgres session, a command prompt will change from $ to # . Lets create a database called finesauces for our project:
postgres terminal
postgres=# CREATE DATABASE finesauces;
Upon a successful database creation, you should see the CREATE DATABASE message. Lets create a dedicated user for our database (replace * with your password):
postgres terminal
postgres=# CREATE USER finesaucesadmin WITH PASSWORD '********';
To follow a Django recommendation regarding PostgreSQL configuration , we will modify a couple of connection parameters for our finesaucesadmin user. This will slightly speed up the database operations so that the correct values do not have to be queried and set each time a connection is established. We will set encoding to UTF-8 , which is expected by Django, transaction isolation to read committed, where the user will see only data committed before the query begins. Lastly, we set the timezone to UTC.
postgres terminal
postgres=# ALTER ROLE finesaucesadmin SET client_encoding TO 'utf8';
postgres=# ALTER ROLE finesaucesadmin SET default_transaction_isolation TO 'read committed';
postgres=# ALTER ROLE finesaucesadmin SET timezone TO 'UTC';
Now lets grant our user unrestricted access to administer the finesauces database:
terminal
postgres=# GRANT ALL PRIVILEGES ON DATABASE finesauces TO finesaucesadmin;
We can quit the PostgreSQL session by typing:
terminal
postgres=# \q
1.2 Git
Git is an indispensable part of modern software development. It is a distributed version control system for tracking changes throughout the development process. It enables us to keep records of any changes made to our code with specific details like what and when was modified. It also allows us to revert back to previous working versions in case anything breaks or gets deleted!
The installation process is pretty straightforward. If you are running Debian or Ubuntu-based Linux distribution, you can use apt-get install git command to install Git. On macOS, a recommended way is to use brew install git command. For Windows, visit https://git-scm.com/download/win and download the latest available version.
Once the Git installation is done, we need to perform a one-time system setup to set a user name and email that will be associated with all our Git commits. Open the terminal and type in the following commands (replace the placeholders with your desired values):
terminal
~$ git config -global user.name ""
~$ git config -global user.email ""
Name and email settings can always be changed by re-running these commands.
1.3 Virtual environment
Virtual environments represent an essential element of Python programming. They are an isolated container which includes all the software dependencies for a given project. This is important because software like Python and Django is installed by default as system-wide. This might cause a severe problem when you want to work on multiple projects on the same computer. What if one project depends on Django 3.1, but a project from the previous year requires Django 2.2? Without virtual environments, this scenario becomes very difficult to manage.