Official Power Up Hosting Blog

Everything about Linux, Windows, and hosting ;)

I am an avid book reader, who enjoys technology as well as writing.

Share


Our Newsletter


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Tags


Twitter


Official Power Up Hosting Blog

The Complete Guide To Install Django And Configure It (Linux & Windows).

Apurva ChodnekarApurva Chodnekar

Contents Of This Guide:

  1. Introduction.
    i) What is a Web Framework?
    ii) What is Django?
    iii) Advantages of Django,
  2. How To Install Django on Ubuntu 16.04.
  3. How To Install Django on CentOS 7.
  4. How To Install Django on Windows 7/8/8.1/10.
  5. Conclusion.

Introduction.

What is a Web framework?

Web framework is a server side application framework that is designed to support the development of dynamic websites.

For example, Django here is the available framework for python.

Python web framework is a code library that makes the life of web developers all over the world much easier.

But how exactly does it help them?

A web framework can also be defined as a collection of pre-defined code that can be used in a program/code to solve a specific issue/problem.

It saves time and increases productivity.

What is Django?

It was named after a jazz guitarist Django Reinhardt.

As you might have already figured out Django is an open source web framework for python.

"Don't Repeat Yourself."

That's Django's motto. It's all about code reuse and keeping the code simple.

Advantages Of Django:

  1. ORM (Object-Relation Mapping): Mapping things manually with all the insert queries will take up a lot of your times, with an ORM like Django all you have to do define how certain classes should be mapped to certain models and let Django do the work.

  2. Integration between the components: The components are tightly integrated meaning, it provides a fully featured and secure authentication systems. It handles stuff like accounts, permissions, sessions.

  3. Admin Interface: Manage the content of your website with an automatic Django interface. Handling users, creating new users all can be done quickly with this smart interface.

  4. Language Support: It provides multi-lingual support. Along with that it also provides locale specific settings.

Now that we have learned a few things about Django let's get started with the installation part.

How To Install Django on Ubuntu 16.04.

We'll begin with the django ubuntu tutorial. It's a step by step tutorial that will guide you through the Django installation on Ubuntu 16.04.

Step 1:

You'll need root access. So, If you haven't already switched to root just execute the following command.

sudo -s

When you hit enter you will be asked for the password.

Step 2:

Prior to installing Django server you need to check the version of python your server is running.

python --version

Make sure that you have 2.7 or a newer version of python. If it's 2.6 or any version lower than that update it. It's now compatible with django.

Step 3

We are going to install a few packages as they are necessary to install Django in Ubuntu environment.

aptitude install python-pip virtualenv virtualenvwrapper

You'll be asked if you want to continue. Enter "y" or "Y" to continue.

Step 4:

We need to create a new directory to store django files.

It's recommended that you create this directory in your home directory.

mkdir django

You can always check to see if the directory has been created by using the ls command.

It will display the content of the current directory.

cd django

Next we are going to create a virtual environment inside this directory.

To do that we'll execute the following command.

virtualenv newproject

To confirm that the newproject virtual environment has been created, you can again use the ls command.

Then set the current directory to bin in newproject.

cd newproject
cd bin

After you have done that list all the contents of bin.

source /home/usr/django/newproject/bin/activate

This will activate your virtual environment.

You'll be able to see that the virtual environment has been activated as your prompt will change accordingly.

Step 5:

We will use the pip command to install django. Get the latest version of Django.

pip install django

If you have a preference for a particular version on Django, you have to execute the the pip install django version command as follows.

pip install django==version

Where version will be replaced by the actual version numbers for example 1.7.11 or any of the versions that you want to install.

pip install django==1.7.11

Step 6:

It's only after you create a virtual environment that you'll be able to move forward with the following step.

After you have installed django the next step is to input django in python.

To enter python prompt type the following command and hit enter.

python

Once we have entered the python prompt we will import django and then press enter.

import django

To check the django version add the following line of code in the prompt.

print(django.get_version())

To exit the python prompt type exit() and then press enter.

Step 7:

Here's what happens if you haven't activated virtualenv.

First, we'll have to deactivate virtualenv.

deactivate

Type the above command and hit enter.

Then start python prompt and try to import django.

When you try to execute the import django command, you'll get the following error.

ImportError: No module named django

Step 8:

Now, if you were to activate the virtual environment again, you would then be able to import django as shown follows.

Execute the following command and press the enter key.

source /home/usr/django/newproject/bin/activate

As we have done before follow these steps. Enter the python prompt, import django, you can use exit() once it works.

python
import django
exit()

Step 9:

Next we are going to create a sample project in django.

django-admin startproject sampleproject1

This will create a new directory with the name sampleproject1

cd sampleproject1/

The "sampleproject1" directory will be where all the sample project files will be stored.

Step 10:

Run the django server:

python manage.py runserver 0.0.0.0:8000

Note:

0.0.0.0 it's the network interface that will allow the other computers in the network to access the web server.

If you don't want them to access your web server you can enter your ip address for only local host access.

8000 is the default port number for django.

You'll get an error or told to run the next command.

Step 11:

Execute the following command to create the necessary dbs and directories.

python manage.py migrate

When this is done try to run the server again.

python manage.py runserver 0.0.0.0:8000

This completes the installation

There's just one more thing left to do.

Step 12:

Open your web browser.

Then enter you local ip address.

For example:

192.168.10.20:8000

You'll be able to check the status of installation here.

If in case you want to uninstall django ubuntu, just execute the following commands.

These commands will only work if you have Django installed.

sudo pip uninstall Django
sudo apt-get remove python-pip

How To Install Django On CentOS 7.

Step 1:

Search for python get-pip on Google.

Find the installation documentation, it should be the first one on the search list.

downloadgetpip1

Then download the get-pip.py from the installation documentation.

downloadgetpip

Step 2:

After you have downloaded the file, find out it's location.

Then open the terminal and set the current directory to that location.

In our case the file is in the downloads directory so we'll execute the following command.

cd Downloads/

Next, set the user as root. We are going to perform some operations that need root permissions.

su

Output:

Password:

To ensure that the file is indeed in the Downloads directory we'll list the files and folders in that directory using the ls command.

Execute the following command next.

python get-pip.py

Now, we can get back to the old user directory.

cd..

Step 3:

Next, we'll install the virtualenv with the following command.

pip install virtualenv

Virtualenv is installed in order to keep the dependencies of different projects in separate places.

It is a tool that's specifically used to create isolated virtual environments.

Step 4:

We won't require the root permissions in the next steps. So, we will switch back to the original user "bob" with the following command.

su bob

Step 5:

In this step we will create a new directory to store the applications and files.

mkdir myfiles

Then use ls to list and check whether the directory has been created.

ls

Step 6:

We will create another directory for storing virtual environment.

mkdir direcvirtual

Set the current directory to direcvirtual using the following command.

cd direcvirtual/

Then create a sample virtual environment.

virtualenv sampleenv

Next, we'll activate the sample virutal environment.

source sampleenv/bin/activate

When you hit enter you'll notice that you have entered the virtual environment prompt.

Step 7:

We can install django with the following command.

pip install django

You can always install a specific version if that's what your project requires.
Currently we are installing the latest version of django.

Open the python shell by executing the following command in the terminal.

python

Once into the the shell import django.

import django
django.get_version()

The above function will get you the version of the django installed.

To exit the python shell type exit() and hit enter.

Step 8:

Now we'll deactivate the sample virtual environment with a simple one word command.

deactivate

If you try to import django in the python shell after deactivating the virutal environment, you'll get an error.

Output:

ImportError: No module named django

Step 9:

To understand things better we will now create another virtual environment.

virtualenv sampleenv1

Activate the new sample environment.

source sampleenv1/bin/activate

When the environment is activate we'll install django 1.7.11.

pip install django==1.7.11

Note:

You have to separately install django for each new virtual environment.

Once django is installed you'll get the message.

Successfully installed django-1.7.11

Next:

We'll open the python shell.

python

Then you can import django and check if the correct version has been installed.

import django
django.get_version()

Output:

'1.7.11'

To exit the pytho shell type exit().

Step 10:

Switch to the myfiles directory.

cd myfiles/

We'll start by creating a new project and then setting the new project directory as the current directory.

django-admin.py startproject newproj
cd newproj/

After you have changed the directory execute the below command.

python manage.py runserver

Click on the link generated or open your browser and then type the link manually.

That concludes django installation on CentOS 7.

How To Install Django on Windows 7,8,8.1, and 10.

This tutorial will not only help you answer the how to install django on windows 10 question but also help you install django if you have Windows 7, Windows 8 or Windows 8.1.

You can follow the same steps for all the windows versions mentioned above.

Step 1:

Download python.

A] Go to www.python.org.

pythonhomepage

B] Select the Downloads tab and find Windows option on it.

windowspythondownload

Click on the Windows option.

It will take you to a page where you'll find the latest python releases for Windows.

Here are the two latest python releases for Windows.

D] We are going to download Python 3 Release as it it the one that works on windows 7,8,8.1 and 10.

latestpythonreleases

Then select Python 3 Release, it will take you to the downloads page.

Scroll to the bottom and find the "Files" section.

pythondownloadpagefiles

Here you will se a list of download version with download link and details.

The bottom 3 in the above image are for 32 bit OS. As we are going to install python on 64 bit OS we will choose one of the 3 x86-64 i.e 64 bit downloads.

We will select the Windows x86-64 web based installer.

Step 2:

Once the file is downloaded start the setup.

Make sure to check the "Add Python 3.6 to PATH" or "Add Python 2.7 to PATH" depending on your OS.

installpythonchecked

Click on the "Customize Installation" part and check whether all the options are ticked.

customizeinstallation

Do not uncheck or deselect any of the options that have been ticked.

Click on "Back".

Then click on Install Now.

The wizard will show the progress of the setup.

installationprogress1

pythonsetupcompleted

After it's installed click on "Close".

Step 3:

Go to start and search for "Windows Powershell".

Right-click on it and select the "Run as administrator" option.

Step 4:

Here's how we will install Django using pip. You can refer the GIF or follow the commands.

The GIF will show you how to install django on windows step by step from here onwards.

how-to-install-django-on-windows-10-1

Before beginning you have to open the Windows Powershell. Go to "Start" or the Metro menu and then search for Windows Powershell.

When you find it. Right-click on it and "Run it as administrator".

1] After opening the Windows powershell, we need to install virtualenvwrapper.

It is used for creating isolated environments.

You can create an isolated environment for each new Django project.
install pip windows

pip install virtualenvwrapper-win

2] Now, we'll create a virtual environment for a project

mkvirtualenv mywebsite

3] To activate the environment mywebsite we have to run the following command.

workon mywebsite

4] Next, we are going to install Django with the pip command

pip install django

5] You can check the django version by executing the following command.

python -m django --version

6] We'll set the current directory to C drive.

cd \

7] The next step is to create a new directory to store the new project that we are going to work on. In this case it will be webdesign.

mkdir newdir

Once you create the directory, set it as the current directory.

cd newdir

8] Now, we need to startproject with the django-admin startproject projectname where we will replace the projectname with a name of the project.

As I have mentioned already it this case it will be webdesign.

django-admin startproject webdesign

9] Set the current directory to the project directory i.e webdesign.

cd webdesign

10] After that is done simply run the

python manage.py runserver

Now that we have completed the installation you might have this question,
how to check if django is installed on windows?

Execute the following command and it will return the version of your django install.

django-admin --version

You can uninstall django with a simple command. To uninstall django windows 10 just execute the following command.

pip uninstall django

Conclusion:

After setting up Django on your OS you can check out the (6 Steps Django Database Tutorial) Create App and Connect a Database. It's specifically for Ubuntu 16.04 users, you can get started with creating an app and connecting it to a database.

It's better to understand what you are working with before jumping into it with your eyes closed. Hence the basics at the beggining at the tutorial.

I am an avid book reader, who enjoys technology as well as writing.

Comments