Python Django Tutorial


Very old draft, anticipate inaccuracies and outdated information.

Installing pipenv

Setup pipenv:

$ sudo pip install pipenv   # system-wide considered dangerous
$ pip install --user pipenv # user-wide safer

$ mkdir my project && cd myproject
$ pipenv install requests
$ cat > <<EOT
import requests
response = requests.get('')
print('Your IP is {0}'.format(response.json()['origin']))

$ pipenv run python # Alternative: $ pipenv shell; python

Setting up Django project

Create project directory and install Django:

$ mkdir django; cd django
$ pipenv --three # for python3
$ pipenv install django
$ pipenv shell
$ python -m django --version

Create the Django project:

$ django-admin startproject mysite
$ cd mysite
$ python runserver      #
$ python runserver 8080 #

# Get rid of warning
$ vi +/ALLOWED mysite/
$ vi +/mystic/
# ALLOWED_HOSTS = ['', '']

Note on Fedora may need to open the port on the firewall:

$ sudo firewall-cmd --add-port=8000/tcp # punch firewall hole for external access
$ python runserver 0:8080     # http://<ext-ip>:8000/

Create the Django App (A Django project can have many apps):

$ python startapp polls
$ cat > polls/ <<EOT
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

$ cat > polls/ <<EOT
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),

$ cat mysite/ <<EOT
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),

$ python runserver 0:8000    # http://<ext-ip>:8000/polls


Project setup:

pipenv install djangorestframework startproject tutorial
cd tutorial/ startapp quickstart

Sync database for first time (password123):

python migrate
python createsuperuser --email --username admin

Useful Links

Installing Databases

Installing sqlite:

$ sudo dnf install sqlite
$ sqlite3 db.sqlite3
sqlite> .schema
sqlite> ^d

Installing MariaDB:

$ sudo dnf install mariadb
$ sudo dnf install mariadb-devel
$ sudo dnf install mariadb-server
$ sudo systemctl start mariadb.service