register now

Blogs

Our blogs

BLOGS

Django: A Beginner Guide for Getting Started with Python Django

Are you new to Python? Or already know some of it and you are looking to extend your knowledge.

Django is a web framework which is entirely built over Python. It is free and follows the Model View Controller pattern where the view relates to the controller and the template relates to the view in the MVC pattern. This can be a bit confusing for the beginners but you will understand the logic as you proceed. There are already lots of popular sites that are running on Django.

Django also provides excellent documentation along with the features and tools some of which include:

A good templating language

Security features like CSRF

Excellent and lightweight server for the development.

In this tutorial, we will show you how you can get your first Django website up and running. Before you start, you need to have Python downloaded and installed on your system. Download and install Python from the official website.

You need to have a basic understanding of Python. Also, you will be running this tutorial on a Linux based system. To ensure that it is fully downloaded, open up your terminal and type:

python

An interactive shell will show up.

Python 2.7.12 (default, Nov 19 2016, 06:48:10)

[GCC 5.4.0 20160609] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>>

To exit this, type Ctrl + Z.

To have a more managed arrangement, it is always advisable to create a directory for your projects.

mkdir folder_name

Then you can cd into the project with cd folder_name

Set up the virtual environment

The next thing we need to do is to set up the virtual environment. A virtual environment helps you to run several versions of Python/Django on your machine. For example, you can have two different Python Django projects running on different versions to avoid them clashing without errors. It is strongly advised to always use a virtual environment.

Top 12 Machine Learning Project Ideas for Students

To set up the virtual environment, you will be needing the python’s package manager pip to do the installation. Type:

pip install virtualenv

After the installation, its time to create a virtual environment that would enable us to use a preferred Django version of your choice.

virtualenv env_name

Note: env_name should be replaced with the preferred name of your environment.

Activating the virtual environment

To activate the virtual enviornment for Linux/Mac OS:

source env_name/bin/activate

For windows:

env_name/script

activate

Installing Django

Now, it is the time to install Django to our machines.

pip install django==1.8

Using ==1.8 gives instructions to the django about the particular version you want to install. In this case, it is 1.8. To go ahead and download the latest version, input pip install django.

Starting a new Project

Now we have the Django running on our systems. Now it is the time to start our first project. In the command line type:

django-admin.py startproject project_name

project_name= name of your project. In this case, we will work with mask_off as our project name. The above command creates a subfolder with the name mask_off and an outline of mask_off.

??mask_off

? ??? __init__.py

? ??? settings.py

? ??? urls.py

? ??? wsgi.py

??? manage.py

Django simplifies our work by creating the above files.

The __init__.py helps Python treat the directories as containing packages so as to prevent the directories with a common name from unintentionally hiding valid modules that occur later on the module search path. In most cases, it is an empty file.

The settings.py file contains all settings your project requires as we progress, we will visit the file often.

The WSGI act as the interface our web server uses to interact with our web application.

Run Server

There is no fun in visiting your own home page. So, let us run our server which also generates a link for us to view our webpage.

python manage.py runserver

Following shows up...

python manage.py runserver

Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.

Run 'python manage.py migrate' to apply them.

July 12, 2017 - 15:19:01

Django version 1.8, using settings 'mask_off.settings'

Starting development server at http://127.0.0.1:8000/

Quit the server with CONTROL-C.

This implies a successful migration. Now, we can successfully run our server with no issues.

python manage.py runserver

We get a success message on our webpage. Now we have our server which is running a webpage. But there is still a lot of work to do before we actually call it a webpage.

So that is all. You can now take more time to practice these steps and become more familiar with them. Practice these steps and become familiar with them.

Things learned from this tutorial:

Setting up the virtual environment

Installing Django

Creating a new project

Basic Project Components

Migrations

Running Server

Looking for the complete Python Django Course? Contact DummyByte. DummyByte offers an industrial training course in Python Django in Chandigarh.