# Django Web Development

Python Django is one of the most commonly used web development platforms. Some of the most popular websites built on Django are:

  • Instagram
  • Spotify
  • Youtube
  • The Washington post
  • Dropbox
  • Pinterest
  • Instagram

# Getting started with Django Ubuntu 20.04

# Install Python

To get started, install python3 development tools on your virtual machine.

sudo apt-get update

sudo apt-get install python3-pip python3-dev libpq-dev postgresql postgresql-contrib

In addition to python development tools, the code above will also install postgesql. We will need this for Django production development and might as well install it now.

Next we need to set up the database, do the following to log you in to postresql.

# Setup Postresql database

sudo -u postgres psql

When you are logged in to the database, run teh following commands.

CREATE DATABASE skolo;

CREATE USER skolouser WITH PASSWORD 'password';

ALTER ROLE skolouser SET client_encoding TO 'utf8';

ALTER ROLE skolouser SET default_transaction_isolation TO 'read committed';

ALTER ROLE skolouser SET timezone TO 'Africa/Johannesburg';

GRANT ALL PRIVILEGES ON DATABASE skolo TO skolouser;

\q

For a list of timezones, visit: Global timezones (opens new window)

# Install Django

sudo -H pip3 install --upgrade pip

sudo -H pip3 install virtualenv

Create a directory and install Django

mkdir skolo && cd skolo

virtualenv skoloenv

source skoloenv/bin/activate

Install packages.

pip install django

pip install psycopg2

Create a new Django project called skoloapp

django-admin startproject skoloapp

Django will create a project structure in this format:

skolo/
│
├── skoloapp/
│   ├── skoloapp/
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   │
│   └── manage.py
│
└── skoloenv/

cd in to the directory

cd skoloapp

Edit the settings file with the following:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'skolo',
        'USER': 'skolouser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

#...
#Move to the bottom of the file
#...

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

# Run the Django app

python manage.py makemigrations

python manage.py migrate

At this stage you want to create a superuser

python manage.py createsuperuser

Collect static

python manage.py collectstatic

Run the App

sudo ufw allow 5000

python manage.py runserver 0.0.0.0:5000

Visit the front end to see the app. Out of the box app should look like this. Django Starter App

# Django Imports

TIP

Great imports to have in your project

pip install django

pip install requests

pip install Pillow

pip install django-resized

pip install django-crispy-forms

pip install numpy

pip install html2text

pip install django-tinymce

If your project is going to be using Django channels

pip install channels

pip install asgiref

pip install channels-redis