'What does django controllers are in views.py

I have followed several django tutorials. I do not know why controllers are stored in a file called views.py. I am confuse with this filename. I am looking for a MVC development. Are there other files in django for "real" controllers ?



Solution 1:[1]

Yes ! Actually it's a design decision and It's described by the guys behind Django Here.

Basically their argument is that, in their opinion,

In our interpretation of MVC, the “view” describes the data that gets presented to the user. It’s not necessarily how the data looks, but which data is presented. The view describes which data you see, not how you see it. It’s a subtle distinction.

a “view” is the Python callback function for a particular URL, because that callback function describes which data is presented.

I entice you to read the entry to get a hold of the overal idea behind the views naming.

About the controllers, Yes again. Mostly though, you can define several layers of the so called Middlewares in django to handle lots of your static logic before/after requests are handled by views, but still, it's the view that plays the main role of a controller in Django.

Middlewares and Views

Solution 2:[2]

The name views.py was a mistake

From an architecture point of view, it was a mistake to call views.py the module where functions receive an HTTP request and produce an HTTP response.

  • Such module is clearly a controller in the MVC sense. (For comparison, the equivalent functionality in the Spring framework is in a class with annotation @RestController or @Controller.)

  • Such module is also a REST adapter if you use the Ports & Adapter architecture style (aka Hexagonal architecture).

  • Such module deals with in-out data (request-response), but it is not a representation of the view displayed to the user. If not, what about an http DELETE endpoint (@api_view(['DELETE']))? What about an http POST endpoint that triggers background processing and has no bearing on what the Web UI shows? The functions in views.py are "handlers" of user actions (i.e., controller in MVC), not the representation of UI data.

I understand that views.py is a convention and you can use other names, but once the tutorial examples and other documentation use that name, it sticks.

Another mistake is when tutorials like this use the name views.py as the single place for all http endpoints. The name views.py is too generic and its very idea is an invitation to violate the Single Responsibility Principle (SRP).

Even simple tutorial examples, should use better names. For example:

  • if you're handling endpoints that deal with customer functionality, call it views_customer.py or customer_views.py (or better yet, customer_controller.py if you're bold to break with the convention and call it for what it is).

Solution 3:[3]

Look at this logically. What do you normally call a text file with placeholders which is filled with other bits of text by supplying variables? You call that a "template", you don't call that a "view". Only in MVC would you think of calling such a thing a "view".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SpiXel
Solution 2
Solution 3 Daniel Roseman