How to set up Django Environment in Linux for beginners
Tags: django, beginners, virtualenv, linux
Published: 2018-03-28 15:53:48

---
cover_image: ![Imgur](https://i.imgur.com/UXkJcTn.png) 
title:  How to set up Django Environment in Linux for beginners
published: True
description: This post will help you set up Django Environment on Linux.Beginner Starter pack on Django structure. 
tags: Django,beginner,virtualenv,Linux
---


<p>When  I started learning Django, finding a tutorial that had everything I needed was pretty hard
So hopefully this tutorial will help you get started!</p>
##### What you will learn 
+ Create a virtualenv <code>test</code>
+ Create a sample project called <code>HelloDjango</code>
+ Create a demo Django app  called <code>accounts</code>
+ Finally configure the <code>settings.py</code>
<p>Let’s dive right in</p>

##### Prerequisites
<p>Make sure you have the following installed on your computer to follow-through.</p>

+ You got Linux Os installed (ubuntu, Debian)
+ Python 3+
+ Django 2.0
+ Virtualenv

##### Django Introduction
<p>According to <a href="https://www.djangoproject.com/" target="_blank">djangoProject</a>,Django is a python web framework for developers looking for a faster way to build web applications and with less code.Essentially, django is for perfectionists with deadlines</p>

##### Why Consider Django?
<p>Please refer to the <a href="https://www.djangoproject.com/" target="_blank">django Documentation</a> for more info.I will give a personal opinion why it is really cool.</p>

+ Inbuilt Security features such as Cross site request forgery (CSRF) protection. 
+ Great Documentation.
+ Faster because It has many inbuilt features such as Admin site, Authentication 

##### Linux Installation
<p>I recommend Ubuntu or Debian .For this specific tutorial, I had mint installed because its light and it’s just a personal preference.</p>
##### Python Installation
<p>Django is a python framework so first ensure you have python installed.The default installed in my Os is 2.7.Check the version of python installed by typing  the following command into your shell:</p>

    $ python --version
    $ python -V  (Mind the capital) 
    $ python2 -V
<p>The above outputs</p>
     Python 2.7.12
<p>To check the version of Python3 </p> 

       $ python3 -V
<p>The above outputs</p>

     Python 3.5.2
  
<p>In this tutorial ,we are using python 3.5 and higher.Type the command below to check which version 3 is installed</p>

    $ python3 --version
<p>You should see something close to this:</p>

    Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
    [GCC 5.4.0 20160609] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
You can type CTRL+D to exit the interactive shell for now.
##### Installing Python
<p>If you do not have python installed,use this command:</p>

    $ sudo apt-get install python3.5
> to install the latest version 3.6 check this <a href=" https://mintguide.org/other/794-python-3-6-install-latest-version-into-linux-mint.html" target="_blank">tutorial</a> 

##### Setting up Virtual Environment
<p>We could just create a Django application on our computer, but to control the packages unique for our application we will use Virtual Enviroment.Virtual Environment is useful when you want to create isolated environments, each running their own versions of packages.We can even create virtual environments with various python versions ! sounds cool.For this tutorial I am going to use virtualenv.</p>
##### Virtualenv
<p>We can install virtualenv using python’s package manager pip.use the command:</p>

    $ sudo pip install virtualenv

> Pip is a useful python package tool that installs, updates, and remove Python packages/libraries used by Django and your other Python apps.Pip come installed with python 2.7+downloaded from <a href="https://www.python.org/" target="_blank">python website</a>.You just have to upgrade it using the command:
       $ pip install -U pip 



<p>You should get something like this if <code>virtualenv</code>is already installed as in my case:</p>

    cindy@cindy-Veriton-M290 ~/Desktop $ pip install virtualenv
    Requirement already satisfied: virtualenv in /home/cindy/.local/lib/python2.7/site-packages
##### Create and name  virtualenv
<p>Once you have successfully installed virtualenv,next we  create the virtualenv test in shell.I like creating my project in documents Folder.So right-click and choose open a terminal here option.Once you open the terminal,type:</p>

    $  virtualenv -p python3 test
<p><code>test</code> is just  a name,you could give it any name.The -p points to the python version you want to use ,in the above case python 3</p>
##### Activate Virtualenv
<p>Now that we have created our virtualenv test,we need to use it to install django and other packages we will need.In order to use our virtualenv,we <strong>must</strong>activate it. change directory to your virtual environment using:</p>

    $  cd test
<p>Then once you are inside the virtualenv directory,activate it as shown below:</p>

     $ source bin/activate
By now you should see your virtualenv in brackets

    (test)  
Below is a sample from my console:

    (test) cindy@cindy-Veriton-M290 ~/test $ 
##### How to check packages installed in the virtualenv you just created
run:
    
     $  pip freeze
##### Django Installation
<p>our <code>virtualenv</code> is running,let's install django .The version of django at the time of writing this article is 2.0.2.</p>

    $ pip install django
Run:

      $ pip freeze 
<p>your console should list the installed packages</p>

    (test) cindy@cindy-Veriton-M290 ~/test $ pip freeze
    Django==2.0.2
    pytz==2018.3
> To specify django version,use:
    $ pip install django==<version>.For example to install django 1.11:
               $ pip install django==1.11

##### Creating Django Project
<p>Django is successfully installed so lets create our project Structure! Type this in your console:</p>

    django-admin startproject HelloDjango
<p>This command creates a template for our project.</p>

    HelloDjango/
        manage.py
        HelloDjango/
            __init__.py
            settings.py
            urls.py
            wsgi.py

<p>The outer <code>HelloDjango</code> is the container holding our project.Its okay to change the name to something else say ‘src'.</p>
<p>The inner<code> HelloDjango </code>contains your site's configurations.I Highly recommend you leave it as it is, at least for now.</p>
<p>Change to project directory</p>

       cd  HelloDjango
>The project directory is the outer HelloDjango containing manage.py file.If you changed its name to src,then change directory to src.

    cd src
##### Make Migrations
<p>We need to create a database and Django comes with sqlite .You are free to use any other databases of your choice.But for this tutorial we will use the sqlite. Change directory to the project root and make migrations by typing the command:</P>

    $ python manage.py makemigrations
     $ python manage.py migrate   
 
##### Test development server
<p> To test if your project is working well,go to your project root directory then type this :

     $ python manage.py runserver
<p>You should see something like this:</p>

    Performing system checks...
    System check identified no issues (0 silenced).
     February 28, 2018 - 18:58:33
     Django version 2.0.2, using settings 'achieng_website.settings'
     Starting development server at http://127.0.0.1:8000/
     Quit the server with CONTROL-C.

<p>When you go to  http://127.0.0.1:8000/,If django was successfully installed you should get a success message"congratulations" </p>
![SucessPage](https://i.imgur.com/F3GEYtx.png)


##### Configuring  Settings.py
<p>We will make some changes on our settings.py.This file help us manage our static files such as css,js, and images.First we going to change <code>Templates DIR</code></p>

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                   ...
                ],
            },
        },
    ]

<p>Add  <code>staticfiles_Dirs </code> as well:</p>

          STATICFILES_DIRS = (
             os.path.join(BASE_DIR, 'static'),
          )
##### Change time zone
<p>Finally,lets go back to our <strong>settings.py</strong>  and change our timezone.I come from Nairobi so my time zone is Africa/Nairobi.Check yours on: 
<a href="https://en.wikipedia.org/wiki/List_of_time_zones_by_country" target="_blank">wikipedia</a></p>

    LANGUAGE_CODE = 'en-us'

    TIME_ZONE = 'Africa/Nairobi'

##### Creating an App

     python manage.py startapp accounts

<p>Your project structure should be like this:</p>

     accounts/
        __init__.py
        admin.py
        apps.py
        migrations/
            __init__.py
        models.py
        tests.py
       views.py

<p>The app creates modules such as <code>models.py</code>,<code>admin.py</code>,<code>views.py</code>,<code>apps.py</code>.
Django implements MVC Pattern</p>
<p>For the app you have just created to be used,add it to <code>INSTALLED_APPS</code> in the <code>settings.py</code>.</p>

    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'accounts',
    )

This Post was originally posted on[achiengBlog](https://achiengcindy.com/permalink/1/)