Web Programming (notes)

last update: 2016-04-30

Web programming in itself is a huge topic with many interesting aspects and infinite opportunities. It requires mastering a series of skills including (X)HTML, CSS, ECMAScript, as well as a database management system and at least one server-side programming language. (X)HTML lays out the structure and content of the documents displayed. CSS takes care of the style and formatting. ECMAScript which is best known by its most important implementation (JavaScript) is responsible for enhancing your sites with behavioral aspects. It gives additional live by allowing to script the site on the client side which is perceived as very fast because it does not require to wait for constant network traffic. It also allows to enrich your sites with animations, form validation and additional controls. One of the more successful libraries built upon JavaScript is jQuery with jQuery UI providing additional widgets, effects and interaction possibilities known from desktop applications.

Nevertheless, it is also necessary to program on the server side. All but the most trivial websites rely on dynamic content stored in a database like SQLite, MariaDB and PostgreSQL. To obtain the data and use it to generate (X)HTML files on the fly, a server-side programming language is required. Furthermore, it can be used to answer queries for additional data sent by client-side ECMAScript. Popular choices for server-side programming are Perl, PHP and Python. To avoid having to take care of each an every detail on the server side, it is typical to use a web framework on top of your programming language of choice. Such frameworks take care of database interaction, security, (X)HTML template generation etc. In the Python world, Django is one of the most popular choices.


Django does an awesome lot for you. At its core, it implements the model-view-controller (MVC) pattern. However, in the world of Django, this pattern is called model-template-view (MTV). This frequently leads to confusion as template in Django lingo corresponds to what a view is in most other languages. Also, a "view" in Django lingo is not the same "view" as in MVC, but corresponds to the controller. What does all this mean?

A model deals with the data model and database interaction of you application. In Django, this is elegantly solved by an object-relational mapping (ORM). That is, you can write typical object-oriented pythonic code without having to worry about database details. The ORM translates your data request to the supported native database interfaces. This makes it possible to change from one database management system to another on the fly without code modifications. A frequent use-case thereof is to code on top of SQLite while deploying on top of PostgreSQL.

The term template refers to literal (X)HTML templates which are populated with dynamic content from the database. All static parts of a page can be stored in one or more templates. If you have a PHP background, be aware that the Django template logic is essentially the inverse of the PHP's includes. Finally, Django's view refers to the programming code (sometimes also referred to as business logic) that connects the data model and templates. This connection goes in both directions - it takes care of how data coming from the data model is displayed and how user input from template forms is stored.

Django is a very progressive project that embraces constant change for the better. However, that means that the provided interfaces are not stable at all. As a consequence, the application of Django requires you to refactor your code with each new Django release. The best way to get started with Django is to browse through their website and give the provided tutorial a go.


When deploying a django package it is important to have proper documentation of all system requirements. In order to be able to do so, it is a common package to develop Django applications in a virtual environment. Packages and Python versions available in your system but not in the virtual environment are considered missing. A virtual environment is essentially a sandbox allowing you to create a Python environment which is independent from your operating system. Inside this environment, you can install different packages or the same packages pinned to specific versions. Subsequently, you can create automatically requirements lists telling which packages your project depends on. There are lots of useful guides wrt. virtualenv, one of them on docs.python-guide.org.

mkdir container && cd container
virtualenv -p python3.4 a_project_venv
source a_project_venv/bin/activate  # check changed bash prompt after command
# installing packages only during the first time
pip install -U pip  # update pip; pip3 is used automatically in the environment
pip install django==1.9.1  # pin django to desired version
which django-admin  # test django version
# set up django project
django-admin startproject a_project && cd a_project
# initialize repository (alternatively you can include your venv)
git init
# work on your django project
python manage.py startapp polls
# continue with the Django tutorial
# ...
pip freeze > requirements.txt  # store environment setup information
# pip install -r requirements.txt  # install requirements if needed
# take care of the remaining VCS stuff by yourself
# ...