Web programming is the best choice for people who have started learning to program and have already gotten in touch with language syntax and programming concepts. And it's a popular career strategy. Web sites or web applications may be created from scratch with raw language, but nowadays there is no need to do it. Every programming language has a lot of frameworks to make the developer's life much easier. Framework gives a structure, a set of rules, and functionality that you can use in building your product. As for Python, there are several popular frameworks: Django, Flask, Bottle, Web2Py, and others. We won't consider the pros and cons of all of them here, as we are starting to learn the Django Framework.
In this topic, we'll start our acquaintance with Django with some words about its history, purpose, and features and will install it on our computer to start using it.
What is Django?
Django is one of the most-used free and open-source Python frameworks in web programming. When you are browsing through boards on Pinterest, reviewing code on Bitbucket, posting photos on Instagram, or making comments with the help of Disqus, you are using Django products. Sites of National Geographic, Open Knowledge Foundation, and Mozilla are made using Django too. You can find out about more well-known services that use Django in this article about websites on Django.
The name of the framework is inspired by the stage name of the famous jazz guitarist Django Reinhardt, so the developers who created many handy add-ons for the framework call their group Jazzband. At first, you won't need all of the tools, but whenever you need more, you can find them on their GitHub account. The first release of the Django framework was in 2008 and it upgrades constantly (once in 8 months on average).
The Django framework provides an API for templating HTML pages, making connections to databases, and using HTTP backend services. Django has many useful web development shortcuts and utilities in one place. This means that developing web applications can be really fast and convenient. Django takes care of user authentication, administration, RSS feeds, and other tasks out of the box. Also, it pays attention to security problems (like SQL injections for example). Remarkable is the fact that due to the special architecture of projects in Django, you can build simple web applications as well as large-scale ones and modify parts of them without harming others. That means the Django framework is scalable and flexible.
Django Web Application Structure
Django implements the Model-View-Template architectural pattern or MVT for short.
The main elements of the pattern:
URL dispatcher: when a request is received based on the requested URL, it determines which resource should handle the given request.
View: receives a request, processes it, and sends some response back to the user. If processing a request requires access to the model and database, then the View interacts with them. It uses a Template to create a response.
Model: describes the data used in the application. Individual classes generally correspond to tables in a database.
Template: represents the View logic as generated HTML markup.
If you find the architecture a bit hard to understand now, don't worry! In the following topics, you'll study it in more detail. For now, let's start with installing Django!
Installation
You can use the Django framework on different operating systems (Linux, Windows, MacOS). The installation process for them differs a bit, and you can look up them on the official website. There are three ways to install packages on the operating system: from source, by Python Package Index (PyPI), or by computer's package manager.
To start working with the framework, choose the version you'll use. Versions are marked with three numbers like X.Y.Z, where X.Y is the feature release and Z is the number of the patch. LTS — long-term support — is a well-known standard in software development. It means that developers will support this version of the framework for an extended time (for Django, it's usually 3 years or more). You can safely update your version to a newer patch release without fear of breaking compatibility with the source code.
A good practice is to install packages into a virtual environment that is isolated from the global to avoid conflicts with libraries and package versions.
As we're starting to learn Django, the best choice for us is the latest version, which is 4.1 now.
Installation on Ubuntu 20.04
Ubuntu 20.04 includes Python 3.8.10 by default. To check a version installed, use the bash terminal command below:
python3 -V
Python 3.8.10Notice, that a Python package manager (pip3) is not available by default on Ubuntu. To install it, use the following command in the bash terminal:
sudo apt install python3-pipThe next step is to install virtualenvwrapper (it's a utility that helps to store all isolated virtual environments in one place, create, copy, and delete them):
sudo pip3 install virtualenvwrapperIt's necessary to set the location of virtual environments to be created. To do this, you need to change the shell startup file .bashrc in the home directory. For this purpose, run the commands:
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "export PROJECT_HOME=$HOME/Devel" >> ~/.bashrc
echo "export VIRTUALENVWRAPPER_PYTHON='/usr/bin/python3'" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrcTo start creating a new virtual environment, use the command mkvirtualenv
$ mkvirtualenv new_django_envIf there is a name of the virtual environment in branches at the beginning of a line in the terminal - it's active and working properly.
And now it's time to install the Django package to our virtual environment, using pip3:
pip install Django==4.0Installation on Windows
If you want to develop Django applications on Windows, you should install Python first, as it's not available by default (the same is for pip3). Look up python.org for the newest version and follow the instructions there.
To check if the package is installed correctly, type the following command in the command prompt:
py --version
Python 3.10.2Then install pip using the ensurepip module that comes with Python:
py -m ensurepip --upgradeTo start working in the virtual environment we should create and activate it:
...\> py -m venv project-name...\> project-name\Scripts\activate.batThe last step is to install Django in a virtual environment:
...\> py -m pip install DjangoInstallation on macOS
MacOS does not include Python 3. You can easily install Python 3 (along with the pip3 tool) with the command:
brew install python3Installing a virtual environment on macOS X is almost identical to Ubuntu. Install the tool with pip3:
sudo pip3 install virtualenvwrapperThen add the following lines to the end of your shell boot file:
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.shThese lines are the same as for Ubuntu, but the boot file in your home directory is named differently - .bash_profile.
After that, reload the boot file by executing the following command in the terminal:
source ~/.bash_profileAt this point, you should see a bunch of scripts running (the same scripts as in the case of installing on Ubuntu).
You should now be able to create a new virtual environment using the mkvirtualenv command.
$ mkvirtualenv new_django_envAnd now it's time to install the Django package to our virtual environment:
pip install Django==4.0Check Installation
After you've installed Django, you'll get django-admin command in your shell (if you've installed Django in a virtual environment, do not forget to activate it). django-admin is a helper that can create a template layout for a new project or add an application to an existing project. You can do all of it manually, but it's much easier to use django-admin for this purpose.
Now you only need to check the version of the installed package:
django-admin --version
# 4.0.2It means that your installation was successful and you can start using Django! You've got the main tool to create your first project, so let's start learning how to use it!
Conclusion
Django framework is a free open-source Python-based toolkit for creating web applications, which helps to simplify development.
Let's summarize what we have learned by this moment:
We know some general information about the Django framework,
We can install pip3, python, and tools for creating virtual environments,
We know how to install Django and start working with it.