Which project structure is correct as per MVC? [closed]
Asked Answered
O

2

3

I noticed frameworks such as flask typically have a module named views to house the:

@app.route('/')
def index():
   return render_template('index.html')

type of definitions and then the jinja2 templates are under the templates directory, however a lot of the node.js frameworks (sails, geddy, locomotive) tend to put the .ejs templates in the views directory instead and have no templates directory.

It seems like this shouldn't be subjective; which is correct as per the MVC model? Should the template files be under the views directory or should the url handler definitions? From what I can tell, the flask application seems to have the correct definition of views; if this is in fact the case, where do flask controllers come in or are these definitions controllers too?

Octoroon answered 8/1, 2014 at 13:13 Comment(2)
There is definitely not a standard as MVC is just a cross language and platform pattern, not a specification for implementation. I usually follow a specific MVC library suggestions for structure.Fixer
Flask isn't an MVC framework. views.py has become a common practice, but it isn't required. templates is the default location for templates, although that can be changed with the template_folder argument.Scolecite
U
2

There is no right or wrong way, the MVC pattern gives you the guidelines for the separation of concerns, it does not tell you how to name or organize things.

Here is how I think the MVC components map to a Flask app:

  • The M is the easiest to understand, as models are pretty clearly specified. What is sometimes not completely clear is that models are not just the database abstractions, the business logic of the application is also part of the models.

  • The V covers the presentation logic. In a well designed Flask application this is handled with templates, so I would say that in terms of MVC templates == V.

  • The C covers a thin layer that updates the M and the V based on input received from the user. In a Flask app these are the request handlers, which have the somewhat misleading name of "view functions". When a view function completes it returns an updated view (rendered template) to the user.

Undecagon answered 8/1, 2014 at 16:57 Comment(0)
A
4

When it comes to frameworks like Flask or Django, they rather use MVT (Model/View/Template) but it is similar to MVC except for terminology. The difference is that the 'T' in MVT stands for Template which is actually the Controller in MVC. So View in Flask is the same as Controller.

So don't think of it as templates vs views but focus on the part that you still have 3 components: Model, Business Logic (view/controllers etc), Visuals (template/html etc)

Adscription answered 8/1, 2014 at 15:7 Comment(0)
U
2

There is no right or wrong way, the MVC pattern gives you the guidelines for the separation of concerns, it does not tell you how to name or organize things.

Here is how I think the MVC components map to a Flask app:

  • The M is the easiest to understand, as models are pretty clearly specified. What is sometimes not completely clear is that models are not just the database abstractions, the business logic of the application is also part of the models.

  • The V covers the presentation logic. In a well designed Flask application this is handled with templates, so I would say that in terms of MVC templates == V.

  • The C covers a thin layer that updates the M and the V based on input received from the user. In a Flask app these are the request handlers, which have the somewhat misleading name of "view functions". When a view function completes it returns an updated view (rendered template) to the user.

Undecagon answered 8/1, 2014 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.