The Django Framework: An Overview
Tags: django, webdev
Published: 2021-04-05 18:18:19

Hi there,

Where have I been? Around the world and now I'm back again :D It's been a while since the last time I wrote. I recently got done with school, and I had to move from my city to a different city. I've been quite busy and lazy too. How I'm able to use the words *busy* and *lazy* in the same sentence is something that I don't want to talk about right now. Why? Because I'm only here to talk about Django. Oh, Django the famous Belgian-born jazz guitarist? LOL NO! I'm talking about Django the **web framework.**

## What is a web framework you ask?

Literally, a framework = frame + work. In other words, a framework provides us with a frame for doing some work. So when we talk about web frameworks, we are talking about the frames that allow us to do some web-related work. But what is a frame you ask?

Seen a picture frame before? it provides a predefined layout for inserting pictures. same with a building frame. Thus, a web framework is more or less like a picture frame. It describes a predefined architecture for building web applications and more. 

And more? Yes, web frameworks also provide reusable components that make developing web applications so much easier. A reusable component provided by a web framework is usually some code that takes care of some repetitive task that's an essential component of all web applications. Essentially, all web applications map client requests to some business logic that modifies or reads some data and then returns a response. Mapping URLs or requests to some business logic, managing an application's data, and dynamically rendering an HTML page based on some data retrieved from the database are three examples of the most common problems all web applications have to deal with. The three problems mentioned are formally called routing, database management, and templating. Web frameworks exist to solve problems like the ones mentioned above and more.

Now, if you don't get anything I have said in the paragraph above, just remember these two things: web frameworks provide us with a predefined architecture and reusable components for building, and running web applications. But that begs the question, if Django is a web framework, how does it discharge the two functions mentioned above?

Django provides a predefined architecture that forces the developer to separate the development of an application into three loosely coupled components: the component that manages an application's data and usually interacts directly with the database, the layer that presents data to or accepts data from the user; usually this is what most users interact with, and lastly the part that sits between the data management layer and the data presentation layer. This layer grabs data, usually, some user input, from the presentation layer, manipulates the data, and then passes it to the data management layer. This layer is also capable of fetching data from the data management layer, manipulating, and formatting the grabbed data in a certain way, and then returning the data as a response to the presentation layer. If you ever feel the need to impress anyone, you could tell them the Django web framework uses a Model-Template-View(MTV) architecture. Where MTV represents the data management, the presentation, and the third layer that sits between the data management and presentation layers respectively. As most of you might have figured out already Django is not really doing anything new. It just puts its twist on the popular MVC software development pattern.

## Oh okay, I get the architecture that Django provides, but what reusable components does it come with?

In fact, the provision of reusable components is Django's greatest strength. Django is tagged as a 'batteries included' framework. It comes fully loaded with lots of extras that make the development of web applications faster and easier. Django comes bundled with components like the Admin backend with a corresponding web interface, Object Relational Mapping(ORM) tool, Routing, and Templating features that allow developers to focus on the application's business logic that changes from application to application and not these mundane tasks that stay the same, more or less.

An admin panel is one of the pivotal components of most modern web applications. In fact, it's unimaginably hard to come across a web application without an admin panel. If the application does not have an admin panel just yet, it's only a matter of time until that basic application needs one. Django recognizes this repetitiveness with admin panels; developers have to make an admin panel for every web application they develop. Repetition isn't Pythonic. Thus, to allow developers to focus on the more important things like the application's core business logic that's more likely to change from application to application, Django comes bundled with an admin full-stack logic. This logic automatically adds an admin panel to every web application created with the Django web framework. Essentially, the Django admin view allows users with the required permissions to manage an application's data from a web interface.

In a similar fashion, Django also recognizes the fact that the data layer is one of the standard components of most modern web applications. As a result, Django also comes with a tool out of the box that makes working with relational databases like Mysql and Postgresql so much easier. Django comes with an Object Relational Mapping(ORM) tool. ORM? Don't worry, ORM is just another computer science jargon that's not too hard to understand at all. 

Normally, how you'd interact with your SQL-based database in let's say Python, is through SQL queries. For example, to get a list of all the users from a database table named Users you'd write an SQL query that looks like this: ```SELECT * FROM Users;``` While this works, it could be really time-consuming as you'd have to write both Python and SQL code. Furthermore, while this is not advisable, sometimes developers use one relational database for development and a different one in the production environment. In situations like that, the developer would have to make modifications to the code that interacts with the database. Object Relational Mapping solves this problem; essentially, it allows you to call and manipulate data from the database using your language of choice, for example, Python instead of SQL. In addition, the ORM also allows switching between relational databases with minimal code modification. In python and many more languages like it, Object Relational Mapping could be achieved using a tool called the Object Relational Mapper. The Django web framework comes with an inbuilt Object Relational Mapper that allows developers to interact with their databases using vanilla Python code.

Furthermore, Django also recognizes the fact that the task of mapping an incoming request(usually a URL path) to some application logic that in turn returns a response could be hard and somewhat repetitive. Again, to allow developers to focus on the main application logic, Django comes with an out-of-the-box solution that routes the incoming request to a Django view function(which is usually some defined logic for executing some task). Django is able to achieve this by allowing developers to specify a list of URLs in a URL configuration file. Each URL in the list is a path to match against the URL, and a target, which is either a view function or a different URLconf. Django is able to correctly route requests to the appropriate view function on behalf of the developer by looking at the list of URLs defined in the URLconf file and invoking the view function mapped to the URL pattern where a match is found.

Lastly, one common problem that arises in almost every web application is the issue of displaying content that changes frequently on an HTML page. In other to make rendering dynamic content within static HTML pages more convenient, Django uses a solution adopted by most web frameworks. Django uses templates to render dynamic content within a static HTML page. A template contains the static parts of the desired HTML output as well as some placeholder for inserting dynamic data. Django and many more frameworks like it is able to achieve this through the use of a template system that it provides out of the box. Django uses a template system called the Django template language (DTL). Django also ships with a built-in backend for the popular Jinja templating engine. The Django template system allows you to specify variables using a "double-handlebars" syntax e.g. ```{{ variable_name }}``` The variable_name will be replaced by values passed in from the view function when a page is rendered. The template system also provides support for expressions ```(with syntax: {% expression %})```  Syntax like the above would allow templates to perform simple operations as iterating list values passed into the template.

Conclusively, Django is a batteries-included web framework that puts its own twist on the popular MVC architecture. Django comes with lots of out-of-the-box solutions like the admin interface, a routing mechanism, an ORM, and a template system that makes developing web applications with this tool very convenient.