liveBook · Manning

Model–view–controller

As explained in Django in Action

Model–view–controller is an architectural pattern that separates an application into data and business logic, presentation, and interaction-control components.

Model–view–controller (MVC) separates a dynamic application into three cooperating parts: the model holds data and business logic, the view presents data, and the controller handles user interaction. This separation helps applications combine stored information, program logic, and web-page presentation without treating the site as a pile of static files. section 1.1.1 section 1.3

In Django, the responsibilities map to familiar Python and framework components:

  • Models represent application data and business logic independently of the user interface. section 1.1.1
  • Views present data; a Django view is a function or class that receives a request and returns a response document. section 1.1.1 section 1.3
  • Controllers provide the input and control mechanism, although Django leaves the boundary between views and controllers deliberately fuzzy. section 1.1.1

A Django request path

A web server passes a URL request through WSGI to Django. Django checks urls.py, follows urlpatterns to a matching path, and calls the mapped view. The view can compose HTML with a template engine and return the resulting document to the browser. section 1.1.1 section 1.3

Figure 1.4 Model—view—controller architectural pattern
figure

Use the architectural diagram to visualize how models, views, and controllers divide application responsibilities. section 1.1.1

How Django in Action covers it

How deep it goes
Defines the three MVC roles and traces Django URL dispatch, but leaves the view-controller boundary intentionally unresolved.
Examples
Uses Django’s urls.py, urlpatterns, request objects, view functions or classes, response documents, and template rendering.
What it assumes
Builds from the distinction between static files and dynamic sites before introducing application architecture.
What you can do after
Evaluate/understand how a Django URL becomes a rendered response through routing and a view.
How it connects
Connects models, views, controllers, URL routing, templates, HTTP requests, WSGI, and single-page applications.

Also explained in

Compare how 4 books explain Model–view–controller →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.