Question

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?

Était-ce utile?

La solution 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.

Autres conseils

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)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top