facebook

Learn to Install Django and Set Up project on Ubuntu

By Suman Saha

Learn to Install Django and Set Up project on Ubuntu

Django is a free and open-source web framework which is written in Python. Django can be installed on a server in many ways, mainly there are three different ways to install Django

 

Django installation with pip.
Install Django with virtualenv.
Install Django from it’s github repository.

 

After installing Django, I will show you the first steps to start a new project with the Django web framework.

Let’s start the installation process

 

Step 1 — Install Python and pip

first, we need to update the local APT repository to install Python

 

$ sudo apt-get update && sudo apt-get -y upgrade

 

After this we can install Python 3 by using the following command:

 

$ sudo apt-get install python3

 

After installing Python, we need pip in order to install packages from PyPi, Python’s package repository.

 

$ sudo apt-get install -y python3-pip

 

Step 2 — Install virtualenv

 

virtual environment(virtualenv) is where user can install software and Python packages, this will isolate the installed software and packages from your machine’s global environment. It will prevent conflicting packages or software from interacting with each other.

For installing virtualenv, we will use the pip3 command

$ pip3 install virtualenv

 

After the execution of this command, virtualenv is successfully installed in your computer

 

Step 3 — Install Django

Option 1: Install Django Globally with pip

 

As we have already installed pip. Now we can use the pip command to install python packages. now install Django on our server using the following pip command:

 

$ pip install django==1.10

 

We set django==1.10 to get a specific version. If you want some other version, just change the number

 

Option 2: Install Django with virtualenv

 

Virtualenv is already installed Now we can use the virtualenv command to create a new environment with python3 as default python version. Now create a new environment “myenv” with python3 as the python version and pip3 for the Django installation.

 

virtualenv –python=python3 myenv

myenv is the name of the environment. The command will create a new directory called myenv which contains the directories bin, include and lib. The virtualenv has been created, now let’s log into the new environment with the command below:

source myenv/bin/activate

Or you can run the following command

myenv/bin/activate

Next, install Django in the newly created virtual environment

 

pip install django==1.10

Option 3: Install Django from it’s github repository.

 

If you want the latest version of Django, then you can install directly from the source.

Now I will show you how to install Django manually from the Django Git repository. First Install git using the following command below:

apt-get install git -y

Next, create virtual environment for python and activate it:

virtualenv –python=python3 django-git

source django-git/bin/activate

Then clone the Django git repository with the command below:

cd django-git

git clone git://github.com/django/django django-dev

Install django with this pip command:

pip install -e django-dev/

-e =  Install a package in editable mode or a local package.

Step 4 — Creating a Django Test Project

In this step, we will install Django inside a virtual environment and then start our first project with Django.

Install virtualenv on the server and create a new environment named ‘newdjango’ :

pip install virtualenv

virtualenv –python=python3 newdjango

Now go to the ‘newdjango’ directory and activate the virtual environment, then install Django with the pip command:

cd newdjango/

source bin/activate

pip install django==1.10

Next, create a new project called ‘myblog’ with the django-admin command:

django-admin startproject myblog

Go to the ‘myblog’ directory and run the ‘manage.py’ file :

cd myblog/

python manage.py runserver

The runserver option will create an HTTP connection with python on localhost IP and port 8000.

Now check from your browser: localhost:8000

 

Next, we will configure the Django admin. A database for a superuser will automatically generated by Django. Before we create the superuser, run the following command :

python manage.py migrate

migrate: make adds the models (adding fields, deleting etc.) into the database scheme, the default database is sqlite3.

Now create the admin/superuser:

python manage.py createsuperuser

 

Username (leave blank to use ‘root’): admin

Email address: admin@mydjango.com

Password:

Password (again):

Superuser created successfully.

Now the Django superuser has been added, now you can execute the runserver command, then go to the browser and visit the django admin page

Visit Django admin page: 192.168.1.9:8000/admin/. This will show the login admin login page, now log in with username admin and your password, you will see the admin page.

So, now Django has been successfully installed inside a virtual environment and we’ve created a sample Django project named ‘newdjango’.

 

Suman Saha Subscriber
Web Developer , Openweb Solutions

I am working in Openweb Solutions as a web developer. Besides coding, blog writing is a passion for me

Posts created 6

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
shares