Running Django on Ubuntu

My server configuration

Distributor ID: Ubuntu
Description: Ubuntu 16.04.6 LTS
Release: 16.04
Codename: xenial
Virtualization type: OpenVZ
Memory: 3GB

Python 3 Installation

Check the latest version available.

sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.7
sudo apt install python3-pip
sudo apt install python3-venv python3.7-venv
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1
sudo update-alternatives --config python
# choose python3.7 as primary
sudo apt install virtualenv

Verify installation by running this; python 3.7 latest version should be executed


MySQL Server Installation & Initial Setup

In my case, MySQL server is not prepacked in the distribution so I need to install it manually.

sudo apt install mysql-server
mysql_secure_installation  # secure mysql access
sudo apt install libmysqlclient-dev

Setup new user & database on mysql

Username: aitvapps
Password: 4!tv4pp5
Tick Create database with same name and grant all privileges box.

Django Installation & Initial Setup

Create virtual environment

cd /usr
python3.7 -m venv aitvapp
cd aitvapp
source bin/activate

Django preparation

pip3 install django
sudo apt-get install python3.7-dev
pip3 install mysqlclient
cd /var/www/deploy
django-admin startproject aitvapp
cd aitvapp
python startapp users

Configure first setting

Edit users/

from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    # add additional fields in here

    def __str__(self):

Edit users/

from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser

class CustomUserCreationForm(UserCreationForm):

    class Meta(UserCreationForm):
        model = CustomUser
        fields = ('username', 'email')

class CustomUserChangeForm(UserChangeForm):

    class Meta(UserChangeForm):
        model = CustomUser
        fields = ('username', 'email')

Edit users/

from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin

from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser
    list_display = ['email', 'username']
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'username', 'password1', 'password2')}
    ), CustomUserAdmin)

Pages: 1 2

Leave a Reply