How to set up Django Environment in Linux for beginners

· 6 min read

djangobeginnersvirtualenvlinux
Prerequisites
  • You got Linux Os installed (ubuntu, Debian)
  • Python 3+
  • Django 2.0
  • Virtualenv
Django Introduction
Why Consider Django?
  • 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
$ python --version
$ python -V  (Mind the capital) 
$ python2 -V
   $ python3 -V
 Python 3.5.2
$ python3 --version
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
$ sudo apt-get install python3.5

to install the latest version 3.6 check this tutorial

Setting up Virtual Environment
$ 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 python website.You just have to upgrade it using the command: $ pip install -U pip

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
$  virtualenv -p python3 test
$  cd test
 $ 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
$ pip install django

Run:

  $ pip freeze 
(test) cindy@cindy-Veriton-M290 ~/test $ pip freeze
Django==2.0.2
pytz==2018.3

To specify django version,use: $ pip install django==.For example to install django 1.11: $ pip install django==1.11

Creating Django Project
django-admin startproject HelloDjango
HelloDjango/
    manage.py
    HelloDjango/
        __init__.py
        settings.py
        urls.py
        wsgi.py
   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
$ python manage.py makemigrations
 $ python manage.py migrate   
Test development server
 $ python manage.py runserver
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.
Configuring Settings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
               ...
            ],
        },
    },
]
      STATICFILES_DIRS = (
         os.path.join(BASE_DIR, 'static'),
      )
Change time zone
LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Africa/Nairobi'
Creating an App
 python manage.py startapp accounts
 accounts/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
   views.py
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 onachiengBlog