Source: Django Project
The above words couldn't be truer. I couldn't count the instances wherein Django has been a tremendous help. Django is a high-level web framework which was created for quick web project development. It lets you build apps on a much faster rate with less code. This is my go-to web framework every time I'm tasked with a project that already had a deadline way before it is delegated to me. For those who are interested in learning Python as a server-side language, these series of tutorials are just for you. This will be part one of a multi-series tutorial wherein we are going to build a single page application using Django (RESTful API) + React and Redux then finally deploy it to the cloud (Heroku).
At the end of this tutorial, you are going to be able to:
This tutorial will assume that you already know Python and have it already installed. You should also know the basics like what is a web framework and what are packages or modules. For now, what we will discuss are basic topics. However, we will go into the more difficult lessons as we go along.
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. It鈥檚 free and open source. Visit the official documentation for more information.
Usually, your python projects often use packages or modules that are not part of the standard library. Projects will sometimes need a specific version of a package in order to work. You will absolutely need it to be accurate to avoid conflicts and bugs or to avoid using an obsolete version of the library's interface.
If we are going to use the Python dependencies installed in our computers, conflict between different projects will arise. For example, app A needs version 1.0 of Z library. We then also try to use app B which uses version 2.0 of Z library. Currently we have 2.0 of Z library. This means that app A will not run in our desktop environment. This situation is exactly why virtual environments were made.
A virtual environment is a self-contained directory tree that contains a specific version of Python plus the additional libraries that we want. And since virtual environments are created on the fly, we can create multiple different virtual environments. This means that different applications can use different virtual environments. In solving the problem above, we just create separate virtual environments for app A and app B and install the required packages separately.
The images below tries to show the situation with and without a virtual environment.
In this tutorial, we will be using virtualenv. This is a python package that is used to create isolated Python environments. In short, virtualenv is used to create a virtual environment for our Django project.
To install, open the command line and enter the commands below.
$ pip install virtualenv
$ [sudo] pip install virtualenv
If the same message like the one below appears, the installation was a success.
D:\test>pip install virtualenv
Collecting virtualenv
Using cached virtualenv-15.1.0-py2.py3-none-any.whl
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
$ virtualenv env
Using base prefix 'c:\\users\\bryan\\appdata\\local\\programs\\python\\python36-32'
New python executable in D:\test\env\Scripts\python.exe
Installing setuptools, pip, wheel...done.
Note: Windows example is shown here
Folder created
Folder structure inside
While still on the directory where you've installed the environment, activate the virtualenv by using either of the commands below.
For linux:
$ source env/bin/activate
For Windows:
$ env/scripts/activate
We should see '(env)' at the command prompt, letting us know that we're running under the 'env' virtualenv install. Do not close the terminal once done. We need it activated so that our dependencies are installed in the virtual environment and not globally. So any time we install a library, it will only be applied on the virtual environment if the '(env)' is shown. If not shown, then it will install globally.
At any time, just enter the code below to stop using virtualenv.
(env) $ deactivate
This is just part one of a multi-series tutorial wherein we are going to build a full single page application and deploy it on a cloud server. Next, we will be installing Django using virtualenv and create our first 'Hello World' application.