Building a simple Django Web Application: Part 2

This article is the second installment in Building a simple Django Web Application. If you haven't read the model article please do so here

Before reading this article, you must understand what an HTTP request is. If not, please quickly read through this article.

A view, simply, works as an intermediary link between HTTP requests and models. That may a little too much to take in. Let's take a step back. 

For example, in your web application, lets say a user made an HTTP GET or POST request. The view function ingests this request and specifies, exactly, what data needs to be read or written from the database through the specified model. After fetching or writing this data, it then renders it to generate an HTML page using an HTML template. Finally, it returns the HTML to the user to be displayed through an HTTP response.





The diagram represents the process flow and how the data is handled beginning from the HTTP request at the top to the database (model), rendered template, and back as an HTTP response. 

The single element we've not discussed yet is the urls.py file. In this file, URL mappers are defined to forward the specified URL to the correct view function. Think of this file as a map that URLs go to, to see which view function they're supposed to go to.

Ok, hopefully by now you at least have a vague understanding of what a view function is and what the urls.py file does. Everything will become much more clear when we work through the example. 

So if we've already defined the models, what's left is to define the view functions, write the URL mapper functions and the HTML templates used to render the data. 

An HTML template is a text file that defines its layout. Without going into more detail (you'll understand more from the example), it's important to note that whenever we call on an HTML template, as we did in the render function before, Django will automatically navigate and look for it in a folder called templates in the folder of the app. However, this folder is not automatically included, we must create it. 

Now that we an idea of how Django functions, lets start building our first app here!