"Getting Started With Django" 
Tags: django, python, webdev, beginners
Published: 2017-07-18 20:28:20

---
title: "Getting Started With Django"
published: true
description: 
tags: Django, Python, webdev, beginners
cover_image: https://thepracticaldev.s3.amazonaws.com/i/bvlob62dfk372l070l8p.png
---

New to Python? or already dancing some tango with it and you feel itâ€™s time to dabble into some framework magic for web?

**Meet Django**:
DJANGO is a web framework built entirely on python,itâ€™s free, open-sourced and also follows the Model View Controller pattern, (in this case; Model, Template, Viewâ€Šâ€”â€ŠWhere the View relates to the Controller and The Template relates to the View in the MVC pattern. This part can be a bit confusing to newbies starting out, but along the line youâ€™ll get to understand the logic behind this more :). There are already lots of popular sites running on django, examples are: *instagram, pinterest etc*
![alt text](https://thepracticaldev.s3.amazonaws.com/i/892l0g65tnxh2fyfxhpm.png)
>sites running on Django

Django also provides some excellent documentation here,along with features and tools, some of which include:
- A nice templating language.
- Security features like CSRF
- Excellent lightweight server for development and testing e.t.c

In this tutorial, I will be showing you how to get your first Django website up and running. Before we start, we need to have Python downloaded and installed on our System, to download and install python, click [here](www.python.org) .
Note: You need to already have a basic understanding of python, also Iâ€™ll be running this tutorial on a Linux based system, so most commands would follow suite the linux way. But there will be little or no difference on most.
To ensure itâ€™s fully downloaded, open up your terminal and type in

```python
python
```
An interactive shell shows up:
```python
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, type in **ctrl + z**.

To have a more neater arrangement, itâ€™s always advisable to create a directory for your projects

```python
mkdir folder_name
```

Then cd into the project with: 
```python
cd folder_name
```
(every other step will be carried out while inside this folder)

**Set Up Your Virtual Environment:**

Next thing we need to do, is set up our virtual environment, a virtual environment helps you run several versions of python/django right on your machine (e.g you could have two different python/django projects running on different versions, to avoid them clashing and to give you room to run them both without errors, the virtual environment comes to your rescue. One virtual environment = one python/django version).Itâ€™s strongly advised to always use a virtual environment.
To set up our virtual environment, weâ€™ll be using pythonâ€™s package manager pip to do the installation, type in:

```python
pip install virtualenv
```

After installation,itâ€™s time to create a virtual environment that would enable us use a preferred django version of our choice:

```python
virtualenv env_name
```

*Note: env_name should be replaced with the preferred name of your environment. (I like to name my environments with the django version installed in it for easier recognition).*

**Activating Virtual Environment:**

To activate our virtual environment for linux/Mac OS:

```python
source env_name/bin/activate
```

For windows:

```python
env_name/script
activate
```

**Install Django:**
Now itâ€™s time to install django on to our machine:

```python
pip install django==1.8
```

Using ==1.8 only gives a direction to django about the particular version you want to install, in this case version 1.8. To just go ahead and download the latest version, input pip install django .

**Starting A project:**
Now we have django up and running, itâ€™s time to start up our first project! Yaah!. Still in our command line, type in :

```python
django-admin.py startproject project_name
```

*Note*: project_name = name of your project . In this case, weâ€™ll work with mask_off as our project name.

This creates a sub-folder with the name mask_off and a skeleton structure of

mask_off
â”œâ”€mask_off
â”‚ â”œâ”€â”€ __init__.py
â”‚ â”œâ”€â”€ settings.py
â”‚ â”œâ”€â”€ urls.py
â”‚ â””â”€â”€ wsgi.py
â””â”€â”€ manage.py

Django gives us more ease by creating the above files:
1) the **__init__.py** helps python treat the directories as containing packages; so as to prevent directories with a common name, from unintentionally hiding valid modules that occur later (deeper) on the module search path.In most cases, itâ€™s usually an empty file.
2) The **settings.py** file contains all settings your project requires, as we progress, weâ€™ll visit this file often.
3) The **WSGI** (Web Server Gateway Interface) acts as the interface our web server uses to interact with our web application. Read more about it [here](https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface).

**Run Server:**
Thereâ€™s no fun thing as that of visiting your own webpage, so letâ€™s run our server which also generate a link for us to view our webpage

```python
python manage.py runserver
```

This shows upâ€¦

```python
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.
```

*Notice the warning message about having unapplied migrations? Now letâ€™s do a small but very important talk about migrations;*

**Making Migrations:**
Migrations helps us make changes to our database schema without losing any data, each time we create a new model or make changes to a current one and run migrations, it helps update our database tables with the schemas without having to go through all the stress of dragging and recreating the database ourselves.

To make our migration:

```python
python manage.py migrate
```

An output of this sort should show up:

```python
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying sessions.0001_initial... OK
```

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 like the one below
![alt text](https://thepracticaldev.s3.amazonaws.com/i/nx0e7ceymnty9q5q0512.png)

 >Yaah! our very own webpage.

Now we have our server running and a â€˜webpageâ€™ , but django takes pleasure in reminding us we still have a lot of work to do before we can proudly call this a webpage.

The second part of this tutorial would contain; Creating a new app in our project, working with Urls, Templates, Creating views and Linking pages. For now, take out more time to go through the steps again and again to become more familiar with them. Only practice makes perfect!

Things learnt From this Tutorial:
- Setting up a virtual environment(Downloading, Activiating Virtual env)
- Installing Django
- Creating a project
- Basic project components
- Migrations
- Running Server

Encountered any issue along the line? Let me know! Iâ€™ll be glad to help

*originally posted on my medium publication [here](https://goo.gl/v7Cw8L)*