The Complete Magazine on Open Source

Setting Up Django on Windows

SHARE
/ 10185 0

Jango installation

Django is a Web development framework written in Python. Therefore it has all the advantages of Python. Setting up Django in Linux is pretty straightforward, but Windows is different. This tutorial takes you through the process of installing Django in a Windows environment.

Django is a free and open source Web framework, written in Python, which follows the model-view-template (MVT) architectural pattern. It is maintained by the Django Software Foundation (DSF). Python is a widely used high-level, general-purpose, interpreted, dynamic programming language. Its design philosophy emphasises code readability, and its syntax allows programmers to express concepts in fewer lines of code than is possible in languages such as C++ or Java. The language provides constructs intended to enable writing clear programs on both a small and large scale.

About pip
pip is a package manager for Python. It makes installing and uninstalling Python packages (such as Django) very easy. For the rest of the installation, we’ll use pip to install Python packages from the command line.

Configuring PowerShell
PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command line shell and an associated scripting language built on the .NET framework. To find PowerShell, click Start -> Search for PowerShell -> Right click and select Run as administrator . A pop-up box will ask, “Do you want to allow this app to make changes to your PC?” Click on ‘Yes’.

Once you have completed the above commands, your PowerShell will look like what’s shown in Figure 1.

By using the following command, we can switch out of the system folder:

$ cd ~

RemoteSigned will let you run scripts and configuration files downloaded from the Internet, which have been signed by trusted publishers. So we will use RemoteSigned here.

$ Set-ExecutionPolicy -Scope CurrentUser

PowerShell will ask about the ExecutionPolicy; we are using RemoteSigned execution.
Once you press Enter, PowerShell will inform you about security and ask, “Do you want to change the execution policy?” Press ‘Y’ for yes.

Now we can check if the current user is trusted or not.

$ Get-ExecutionPolicy -List

The output looks like what’s shown in Figure 2.

This confirms that the current user can run trusted scripts downloaded from the Internet.

Figure 1 Opening view of PowerShell

Figure 1: Opening view of PowerShell

Package manager installation (Chocolatey)
Chocolatey is a package manager for Windows (like apt-get or dnf, but for Windows). It was designed to be a decentralised framework for quickly installing applications and tools that you need. It is built on the NuGet infrastructure, and currently uses PowerShell to deliver packages from the distros to your computer.

Note: Inspect all scripts downloaded from the Internet prior to running them on your local system. All of these scripts download a remote PowerShell script and execute it on your machine.

$ (New-Object System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’)

After we inspect the script, we can install Chocolatey using the following command:

$ iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex

The cmdlet iwr allows us to extract data from the Web. This will pass the script to the iex.
The cmdlet iex will execute the contents of the script, running the installation script for the Chocolatey package manager.

Installing Python
To install Python, use the following command:

$ choco install -y python

After the installation is finished, you can check the version of Python installed on your machine by using the following command:

$ python --version

The output should be something like what’s shown below:

$ python v3 . 5 . 1 . 20161002

Along with Python, pip is also installed. To upgrade it to the latest version, run the following command.

$ python -m pip install --upgrade pip

Setting up the environment
One can keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. This solves the ‘Project X depends on version 1.x but Project Y needs 4.x’ dilemma, and keeps your global site-packages directory clean and manageable.

You can set up as many programming environments as you want. Each environment is basically a directory or folder in your computer that has a few scripts in it to make it act as an environment.
To create a new directory named myproject, use the following code:

$ mkdir myproject
$ cd myproject

Once you have entered the directory, you can create an environment, as follows:

$ python -m venv myenv

To use this environment, you can activate it by using the following command:

$ myenv\Scripts\activate

We can now install Django.

Fig 2 Scope of execution policy

Figure 2: Scope of execution policy

Installing Django

Use the following command to install Django:

$ pip install django

Once this is completed, use any editor to create Django apps. The most preferred one is PyCharm. You can download it from https://www.jetbrains.com/pycharm.

I hope that helps!