Programming

How Web Frameworks Streamline and Structure Websites

On MVT architecture, URL dispatching, templating and putting that all together

Joaquin de Castro
Jun 19 · 5 min read
Image for post
Image for post

Web frameworks make building and deploying web applications simple and efficient. With them, we can make high-level applications without too much technical knowledge. Nonetheless, it is still important to be able to understand and even appreciate what goes on behind these frameworks.

In this article, I go over some of the processes and concepts that web development frameworks used to make dynamic web applications possible. I do this through the lens of a popular Python web framework Django. But although some of these features are specific to Django, the underlying processes and concepts are widely applicable elsewhere. For along the way, we can also acquire better programming habits and practices.

MVT Architecture

Django employs a Model-View-Template system to structure our web applications and keep our code clean. An important concept in programming is Separation of Concerns. This principle simply means that we, as developers should keep our code organized – separating backend from the frontend, etc. MVT architecture allows us to do just that. The backend data (Models) are kept separate from request handling (Views) and the frontend page that the user actually sees (Templates). Models can be thought of as the raw data our application uses, while templates are the (mostly) HTML and CSS that our users can see. And of course, Views are the functions that mediate between the two.

Image for post
Image for post

What makes MVT architecture so powerful is that it keeps our code DRY (don’t repeat yourself) and even makes it reusable. Models, for example, are just classes, hence they can be instantiated in different views. Additionally, Django and other frameworks allow entire apps to be reused. “Apps” in Django are parts of a project. But these apps can function independently of each other, and can hence be employed in multiple projects.

Image for post
Image for post

Another key advantage of MVT is security. We wouldn’t want to provide users with potentially malicious intent a one-way path to our models and database. The views in between allow us to insert authentication and validation. These act as gateways that protect our web app, thereby increasing security.

💡Key Takeaway: The MVT architecture consists of parts which can function independently of one another. This allows for Separation of Concerns, DRY code, and code reusability.

Url Dispatching

In static websites, URLs point where to retrieve the file to be rendered in the site directory. For instance, mywebsite.com/blog/posts.html points to the posts.html page on the blog folder. While this is fine, there is also no option to customize how our URLs appear, and no way to add more functionality to them. Moreover, if we wanted a page for every post, we would need to make a new file for each new post. That is both tedious and unnecessary.

Url dispatching solves both of these problems. A URL dispatcher allows us to dictate how our URLs will look along with which view it will render. Through this, we can also use variables that function depending on what arguments are passed. Take the blog post example. With this feature, we can simply input a primary key (post/1) or a slug (post/this-is-a-post) variable to render a different page depending on the arguments.

One thing we should avoid as developers is hard-coding URLs. This is because URLs are bound to change, and should be flexible to that. That said, we wouldn’t want to retype every instance of a URL when we change it. To solve this, Django has a name argument that we can reference instead of the actual path. This allows us to change our URL patterns without having to edit the places where they were referenced

💡Key Takeaway: Url Dispatching allows for fully customizable url paths and arguments, and eliminates hardcoded urls.

Templating

With URL dispatching, we mentioned how we can create URLs for multiple pages with only a single path. But we would still have to make a new .html file for each post, right? Well, we can make use of another feature called templates. As the name suggests, templating lets us make a single template for all our post pages and simply put python variables in place of the information, which will be provided by the associated view. (See again the beauty of MVT architecture?)

Image for post
Image for post

Although the example above is relatively simple, templates can be extremely powerful. They make it easier to render forms and pass user input. With templating, it’s a much simpler matter to integrate backend functionality to the frontend web page.

💡Key Takeaway: All that said, templating makes our website much more dynamic and efficient

Putting It All Together

To see how this all comes together, let us see the entire process from URL to web page.

Url

Here our URL dispatcher matches the searched URL with our paths.

Once found, this path sends the associated view the request, along with the arguments (if any) in the URL.

Views

The view would then fetch the information the template might need to render the web page. This includes not just the models, but form classes, user authentication, and request validation. A view doesn’t have to do all of these. It all depends on the type of request and what function the web page will serve.

Models

Our view then interacts with the models, which in turn gets data from the database. Remember the argument(s) passed from the URL? Our model can use this to filter from a set of data and provide only the needed information.

Validation also occurs in the model. For instance, a field has a certain type and can have parameters specified. User input is valid only if it fits the field type and complies with the parameters.

Back To Views

This is not completely accurate, as the context is defined differently in class-based view

The information from the database and models is then passed back to the views, usually as a dictionary called “context”. The view can also provide other information or variables that the template would need.

Templates

And finally, we get to the template. Recall, that it is this template that our users can actually see in the browser. The placeholders in the template are filled with the values in the context, which it got from the views.

And the final web page is ready.

Finally!

Frameworks like Django and the processes behind them allow anyone to build not only streamlined web applications, but also good coding habits along the way.

Glossary and Notes

Django: a popular python-based open-source framework

MVC & MVT architecture: Django uses MVT architecture, while most other frameworks use a similar MVC (Model-View-Controller) architecture.

Web Development Framework: provides services and resources to help with the development and deployment of a web application

Towards AI — Multidisciplinary Science Journal

The Best of Tech, Science and Engineering.

Sign up for Towards AI Newsletter

By Towards AI — Multidisciplinary Science Journal

Towards AI publishes the best of tech, science, and engineering. Subscribe with us to receive our newsletter right on your inbox. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Joaquin de Castro

Written by

Lead Developer, Co-founder at MindReform | Math, Web Dev, Scraping | www.joaquindecastro.gq

Towards AI — Multidisciplinary Science Journal

Towards AI is a world’s leading multidisciplinary science publication. Towards AI publishes the best of tech, science, and engineering. Read by thought-leaders and decision-makers around the world.

Joaquin de Castro

Written by

Lead Developer, Co-founder at MindReform | Math, Web Dev, Scraping | www.joaquindecastro.gq

Towards AI — Multidisciplinary Science Journal

Towards AI is a world’s leading multidisciplinary science publication. Towards AI publishes the best of tech, science, and engineering. Read by thought-leaders and decision-makers around the world.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store