The MVC Design Pattern in Django

Julius Omoleye
2 min readMay 30, 2022

Before we go into the nitty gritty of Django’s MVC design pattern.
Let’s have a look at what the MVC Architecture is all about. Let’s pretend you go to a restaurant and place an order; the waiter or waitress delivers your order to the kitchen, where the meal will be cooked by the chefs. After a few moments, the waiter returns to the kitchen to pick up and serve your order. Taking the preceding scenario seriously, the next procedures guarantee that you are served your dinner.

  • The waiter or waitress receives your order and delivers it to the kitchen.
  • The chefs are in charge of preparing and serving the meal.
  • The waiter returns with the food.
  • You’re about to be served!!!

Using the preceding example Let’s now look at Django’s MVC Design Pattern.

MVC stands for Model View Controller, and the MVC design pattern is used by Django, a famous Python web framework.

  • Your projects’ foundation is the Model. This is the source of the data.
  • The View is the section of the program where the user interacts with it. The view receives data and sends it out.
  • The Controller is the module in charge of all user requests. Getting data from the model, manipulating it, and displaying the templates

Using our Restaurant as an example

a. We may refer to our Model as the Kitchen because that is where the data (food) comes from.

b. We can also refer to our Controllers as Chefs because they manage user requests (prepare the food).

c. Finally, we may refer to our View as the Attendant since he or she is the one that connects with the user (customer), takes orders, and serves them.

The urls.py(The View) module in Django contains a URLConf for your project, which is a collection of all potential url patterns for the project.

When a user types a URL into a browser, the browser makes a request to the server where your app is hosted, which is also known as an HTTPRequest(A web request and web answer is a way for computers to interact with one another). When the server receives an HTTPRequest, it will send the url to the urls.py module, which will search the requested url against all url patterns in urls.py, and if a match is found, it will send the request to the view function or class attached to it, which must be defined in your app’s views.py(The Controller) file.

The view would conduct database queries, data modifications, and then deliver the results as a Web Response (HTTPResponse) According to tradition, The Model is specified as a class in the models.py file.

If you still have trouble understanding the principles after reading this, you may look at the Django documentation here, which is a great resource.

Thank you for taking the time to read.

--

--

Julius Omoleye

Backend Systems and Devops with Python 🦄 AI and ML freak👻