Learn » Django » Overview.md

Django and Dynamic Web Applications

NOTE: This tutorial builds off of the work done in the static Population Tracker. It is recommended to familiarise yourself with the content covered in that tutorial before continuing on with this one.

Django is a popular web-framework written in Python that greatly simplifies the development of dynamic web applications. Web applications can be visualised as a stack, with multiple layers stacked on top of one another. The major layers are explained in greater detail below.

Level 0: Database

At the very base of the stack is a database. Django provides us with an ORM (Object Relational Mapper) to allow us to access the data stored in the database as though it consisted of native Python objects. This means that we don't have to manually execute database queries to fetch data; instead, we write some Python code and Django automatically converts the Python into database queries.

In the context of our population tracker, the mapping of country to population will be stored in one table of our database. We will then use the Django ORM to both access existing data, and create new population data in the database.

To provide this Python object interface to database objects, we will write a few Models. In Django, Models are Python classes that logically represent our data, which are then used by the application layer, which we will see next.

Level 1: Application

The next layer consists of the logic of the website. Called the "Application Layer", it defines what different parts of the site do and how they interact with each other. It utilises the Models defined earlier to create new instances of the Models, build relations across Models etc.

In Django, the base unit of the Application layer is called a View. A View is a Python function which usually maps to a page on the website and defines the possible behaviour of that page.

Views are where the dynamic nature of a website is expressed. Within a view we can define different behaviour for different inputs received from the user and/or the database. For example, we can add an 'Edit' button to each of our entries in the population table, and write a view to handle this button click and provide us with a form to change the existing data displayed in the table.

Level 2: Presentation/Template

The logic defined in the views isn't complete with some way to display content on the page that the user can see and interact with. In a website, content is normally represented as HTML markup which the browser renders with the defined styles. When we built our static population table in the Static Sites tutorial, we were building this content that the user can see.

However, just having static content isn't good enough for many web applications; we want dynamic content on the page as well. For example, if we had a login system for the population tracker, we want to display the signed in user's name at the top of the page. So how do we go about displaying this information that can change depend on the person logged in? The answer lies in Django's Template Language.

Django allows us to use templates in HTML that can have different values based on the data returned by the view associated with that page. So with our login system, we can add a {{user}} template in the HTML, and as long as the view gives us a username for the currently logged in user, this template will be replaced by the actual username.

Level 3: Router

Now that we have a database, application logic and templates ready, the last part of the stack is the URL Router. Simply put, the router defines which URLs of the website map to which views in the application.

In our population tracker, we want a page to display population data for all countries. We can have a URL /all for this and create a view called all_countries_population to return a page with this data. Then in the router we want to map the /all URL to the all_countries_population view. Once this is done, when a user goes to the /all page, the content they see will be provided by the all_countries_population view.