Django Environment setup

How to Setup Environment to develop Django Application

In this exercise, we will set up a Django environment. We’ll install Python 3, pip 3, Django, and virtualenv so as to furnish you with the tools necessary for creating web applications with Django.

Step 1: Install Python and Pip

Django is written in 100% Python code, so you’ll have to install Python on your system. Most recent Django version requires Python 2.6.5 or higher

In case you’re on one of the most recent Linux or Mac OS X distribution, you likely have Python installed. You can confirm it by typing python command at a command prompt. If you see something like this, then Python is installed.

$ python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2

Otherwise, you can download and install the latest version of Python3 from here

Now that we have Python 3 installed, we will also need pip in order to install packages from PyPi, Python’s package repository.

$sudo apt-get install -y python3-pip

To verify that pip was successfully installed, run the following command:

$ pip3 -V

You should see output similar to this:

$ pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

Now that we have pip installed, we have the ability to quickly install other necessary packages for a Python environment.

Step 2: Install virtualenv

virtualenv is a virtual environment where you can install software and Python packages in a contained space, which disconnects the installed software and packages from the rest of your machine’s global environment. This advantageous detachment keeps clashing packages or software from interacting with one another.

To install virtualenv, we will utilize the pip3 order, as demonstrated as follows:

$ pip3 install virtualenv

Once it is installed, run a version check to verify that the installation has completed successfully:

$ virtualenv --version

We should see the following output, or something similar:

virtualenv 20.0.20 users/hp/appdata/.local/lib/python3.5/site-packages/virtualenv/__init__.py

Likewise, you can also install virtualenv using easy_install.

To install using easy_install, open command prompt, and run it as an administrator.

Step 3: Install Django

Installing Django is exceptionally simple, however, the steps required for its installation relies upon your system. Since Python is a platform-independent language, Django has one package that works anywhere regardless of your operating system.

You can download the most recent version of Django from here.

UNIX/Linux and Mac OS X Installation

You have two different ways of installing Django if you are running Linux or Mac OS system

  • You can utilize the package manager of your OS, or use easy_install or pip whenever installed.
  • Install it manually utilizing the official archive you downloaded previously.

We will cover the subsequent choice as the first relies upon your OS distribution. If you have chosen to follow the first choice, simply be cautious about the version of Django you are installing.

Suppose you got your archive from the link above, it ought to be something like Django-x.xx.tar.gz:

Extract and install.

$ tar xzvf Django-x.xx.tar.gz
$ cd Django-x.xx
$ sudo python setup.py install

You can test your installation by running this command

$ django-admin.py --version

If you see the current version of Django printed on the screen, then everything is set.

Note − For some version of Django it will be django-admin the “.py” is removed.

Windows Installation

We expect you have your Django archive and python installed on your PC.

To begin with, PATH verification.

On some version of (windows) you may need to ensure the Path system variable has the path the accompanying C:\Python34\;C:\Python34\Lib\site-packages\django\bin\ in it, obviously relying upon your Python version.

At that point, extract and install Django.

c:\>cd c:\Django-x.xx

Next, install Django by running the following command for which you will need administrative privileges in windows shell “cmd” −

c:\Django-x.xx>python setup.py install

To test your installation, open a command prompt and type the following command −

c:\>python -c "import django; print(django.get_version())"

If you see the current version of Django printed on the screen, then everything is set.

OR

Launch a “cmd” prompt and type python then −

c:\> python
>>> import django
>>> django.VERSION

Step 4: Database Setup

Django supports a few significant database engines and you can set up any of them dependent on your comfort.

You can refer to separate documentation to installing and configuring a database of your choice.

Note −MongoDb and GoogleAppEngine Datastore are NoSQL databases.

Step 5: Web Server

Django accompanies a lightweight web server for creating and testing applications. This server is pre-configured to work with Django, and all the more critically, it restarts at whatever point you alter the code.

Although, Django does support Apache and other popular web servers, for example, Lighttpd.