Geek Culture
Published in

Geek Culture

Learning Django: REST Framework and MVT Architecture

Defining the Django REST Framework (DRF), Django’s MVT design pattern, and how it compares to the more typical MVC architecture

Photo by Faisal on Unsplash

This week to experiment with a new backend framework, I decided to build the usual to-do app powered by a Django on the backend and React on the frontend. In the process, I started learning more about the Django REST Framework, a powerful toolkit for building Web APIs needed on the server side, as well as its Model-View-Template design architecture.

What is Django?

Before diving into some core concepts related to building Web APIs with Django, let’s define what Django is and how it is useful for developers.

Django is a Python web framework that simplifies common practices in web development, taking care of a lot of the hassle by offering an ecosystem of stable libraries supporting common development needs.

What is REST?

REST, or Representational State Transfer, is an architectural style for providing standards between computer systems.

The MDN Web docs explain it as so:

The basic idea of REST is that a resource, e.g. a document, is transferred via well-recognized, language-agnostic, and reliably standardized client/server interactions. Services are deemed RESTful when they adhere to these constraints.

Following this style, code on the frontend/client side and backend/server side can be implemented independent of each other, without changing the operation of the opposite service. Different users can hit the same REST endpoints, perform the same actions, and get the same responses.

Under the REST framework, clients, or browsers, send a request to retrieve or edit data, and a server receives the request and sends back a response. The 4 HTTP verbs used to interact with a datapoint/resource in a REST system, defining which operation to perform, include: GET, POST (create a new resource), PATCH/PUT (edit a resource), DELETE.

A REST API (also known as RESTful API) is an application programming interface (API) that conforms to the constraints of the REST architectural style. In these APIs, paths are designed to help the client understand what is happening due to their request (i.e. locating a specific piece of data/resource).

After a request is made by the browser/user via a RESTful API, it transfers a representation of the state of the resource to the requester. This information is delivered in one of several formats via HTTP most commonly in JSON (Javascript Object Notation).

The Django REST Framework (DRF)

Creating REST APIs from scratch takes time and effort, however, Django offers an installable extension which provides functionality to get started creating APIs faster.

DRF Installation

After getting a Django project and app up and running (this first and second part of a seven part tutorial series provides a solid foundation on how to do so for beginners), run the following command in the Django virtual environment to get started with building Web APIs:

pipenv install djangorestframework django-cors-headers

Once you have installed that from the command line, add both ‘corsheaders’ and ‘rest_framework’to the Installed_Apps list in the included settings.py file.

Django’s Design Pattern

I assumed the Python framework would follow the MVC — model, view, controller — software design pattern, since it is commonly employed in languages such as Ruby, Java, C, C++, and many others. Although the fundamental logic applies, Django instead the MVT — model, view, template — architecture, resting on some key differences worth exploring and understanding.

Having a design architecture like the MVC or MVT places emphasis on separating data representation from the components which interact and process the data.

Model — the interface for the data in an app, it provides the logical structure of the entire app, represented by a database (often a relational database like Postgres or MYSQL).

View — accepts HTTP requests from the browser and returns HTTP responses. It acts as the bridge between the Model and the Template, rendering the template based on retrieving the appropriate data from the database.

Template — the HTML code . Not relevant when working with a frontend framework/library like React. In the MVC pattern, the Template is most comparable to the View.

Compared to the MVC pattern, the Django framework itself handles what the controller is responsible for in the MVC pattern.

This step-by-step explanation of the MVT flow explains it well:

1. The user sends a URL request for a resource to Django.

2. Django framework then searches for the URL resource.

3. If the URL path links up to a View, then that particular View is called.

4. The View will then interact with the Model and retrieve the appropriate data from the database.

5. The View then renders back an appropriate template along with the retrieved data to the user.

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

Integrate your Reliability Toolkit with Your World, Part 1

HelloExperts — Meet Dragan, Product Owner @ Helloprint

Legacy system

Legacy system

If You’re a Shy Bee, You Might Want To Ignore This Newbie Coder Advice

Toddler touching the Apple mouse, looking up at a computer screen. The young one barely reaches over the tabletop.

Extract Timeline Tweets using Tweepy

Python Interview Questions 1

Literate Programming: A Radical Approach to Writing Code with Documentation

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
Sara Bastian

Sara Bastian

full-stack developer exploring how software can solve real-world problems | https://sarabastian.com/

More from Medium

Get start Django project, and its files and directories structure

5 Ways to Optimize Databases in Django

An Introduction to GraphQL in Django