Skip to content

Setup Django projects

The department ImPhys uses Django for database driven web-applications. This web framework is developed in Python and can be used with many database engines.

Typically all Django websites for ImPhys reside on the qiweb.tudelft.nl and are acessabel via this server. Using Apache each site can be accessed via a subdirectory after the hostname in the URL; virtual sites are not used.

This manual describes how to setup this server and how to start a new Django project. As per January 2020 Python 3 is used.

Introduction

These instructions help you in setting up a web-application with Django. It is setup around this tutorial:

You need an recent Debian-based Linux distribution (like Ubuntu) and the installation of Apache2. Furthermore the instructions set up the web-application as an WSGI application without the use of virtual sites. This allows you to have multiple applications running simultaneously on the same hostname and port with different subdirectories. This example is using http://qiweb.tudelft.nl/test.

All instructions in the linux shell are preceded with the prompt denoted by a dollar-sign $. Do not type this character!

Installing Apache 2 and WSGI

$ sudo apt install apache2 libapache2-mod-php libapache2-mod-wsgi python-certbot-apache

Note

Most Linux installations already provide Apache 2.

Installing Python and virtual environment

For Python 3 the package venv is used to create Python virtual environments. For more information:

$ sudo apt install python3 python3-pip python3-venv

Note

depending on your version of the OS Python 3 might already be installed per default.

Also make sure to set Python 3 as the default python if this is not already the case. You can check your default version with $ python --version.

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10

Setting up venv and install Django

The following will setup your virtual environment in the subdirectory env. The environment is activated with source and the Django packages is installed with pip. Finally the Django version is shown:

$ mkdir test
$ cd test
$ python -m venv env
$ source env/bin/activate
$ pip install django
$ python -m django --version

Note

some shells (e.g. fish) indicate in the prompt which virtual environment is active. This is very helpfull when using multiple environments.

Create project and app

Important

Make sure you are running these commands while in the virtual environment!

$ django-admin startproject testsite .
$ python manage.py startapp polls

Now modify polls/views.py, polls/urls.py and testsite/urls.py like described here:

Configure WSGI for project

Create directories for static and media content. Note the link in the static directory to the admin interface of Django. This directory can be found with python -m site --user-site.

Note

The site-packages-directory can be referred to relative to to path its linking from. See the example below.

$ mkdir static media
$ cd static
$ ln -s \
  ../env/lib/python3.6/site-packages/django/contrib/admin/static/admin \
  admin
# Do not use the path above, this is an example and will not work in your environment

Create testsite/wsgi.py:

# =====================
# wsgi.py file begin

import os, sys
# add the testsite project path into the sys.path
sys.path.append('/home/rligteringen/django_projects/test')

# add the venv site-packages path to the sys.path
sys.path.append('/home/rligteringen/django_projects/test/env/lib/python3.6/site-packages/')

# pointing to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testsite.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# wsgi.py file end
# ===================

Modify testsite/settings.py:

ALLOWED_HOSTS = ['qiweb.tudelft.nl',]

Create /etc/apache2/conf-available/djangotest.conf:

Define url test
Define basePath /home/rligteringen/test
Define projectPath ${basePath}/testsite
Define envPath ${basePath}/env
Define mediaPath ${basePath}/media/
Define staticPath ${basePath}/static/
Define userName rligteringen
Define groupName rligteringen

WSGIDaemonProcess ${url} \
        python-home=${envPath} \
        python-path=${basePath} \
        user=${userName} \
        group=${groupName}
WSGIScriptAlias /${url} \
        ${projectPath}/wsgi.py \
        process-group=${url}
<Location /${url}>
        WSGIProcessGroup ${url}
</Location>

Alias /${url}/media/ ${mediaPath}
Alias /${url}/static/ ${staticPath}

<Directory ${staticPath}>
        Require all granted
</Directory>

<Directory ${mediaPath}>
        Require all granted
</Directory>

<Location "/${url}/secret">
    AuthType Basic
    AuthName "Top Secret"
    Require valid-user
    AuthBasicProvider wsgi
    WSGIAuthUserScript ${projectPath}/wsgi.py
</Location>

<Directory ${projectPath}>
        <Files wsgi.py>
                Require all granted
        </Files>
</Directory>

Follow the instructions in the Django tutorial

Before following the tutorial change the timezone in testsite/settings.py:

TIME_ZONE = 'Europe/Amsterdam'

And then:

  • change the url in testsite/settings.py:
STATIC_URL = '/test/static/'
  • store all changes in the database and always reload apache after a database change:
$ python manage.py migrate
$ sudo systemctl reload apache2

Continue with the tutorial here: https://docs.djangoproject.com/en/2.2/intro/tutorial02/

Modify model

$ source env/bin/activate
<edit model>
$ python manage.py makemigrations
$ python manage.py migrate
$ sudo systemctl restart apache2

Start Python shell (interactive)

This allows you to do some testing inside a Python shell with access to all databases.

$ source env/bin/activate
$ python manage.py shell
>>> from <appname>.models import <models>
>>> <model>.objects.all()
>>> <model>.objects.filter(<field>=<value>)

Last update: 2021-05-26