Flask-framework: MVC pattern

Flask is actually not an MVC framework. It is a minimalistic framework which gives you a lot of freedom in how you structure your application, but MVC pattern is a very good fit for what Flask provides, at least in the way that MVC pattern is understood today in the context of web applications (which purists would probably object to).

Essentially you write your methods and map them to specific route, e.g.:

@app.route("/")
def hello():
    return "Hello World!"

No view or model there, as you can see. However, it is also built on top of Jinja2 template library, so in a realistic app, your method (which acts as a controller) looks like:

@app.route("/")
def hello():
    return render_template('index.html', username="John Doe")

Here, you use index.html template to render the page. That is your view now.

Flask doesn’t prescribe any model. You can use whatever you want – from complex object models (typically with using some ORM like SQLAlchemy) to simplest thing which fits your needs.

And there you have it: MVC

Leave a Comment