artvolk
artvolk

Reputation: 9518

Controller classes in Flask

I'm digging into Flask with ASP.NET MVC / PHP MVC frameworks background. I'm not sure what is the prefered way of grouping actions (in the sense "function that process request").

In ASP.NET MVC actions are methods, controllers are classes. Controllers can be grouped into Areas. But flask application can be divided into modules and\or blueprints (looks like ASP.NET MVC Areas to me). Neither way uses classes, why?

Please check the great answer below, another option is Flask-Classy

Upvotes: 6

Views: 9368

Answers (1)

tbicr
tbicr

Reputation: 26070

Just my opinion.

At first in most cases you do not need classes. I can't found cases where I really need classes to just connecting dispatchers to endpoints (I do not told about complex cases where you will use decorators or Pluggable Views). Do you have many instances of your controllers in ASP.MVC and etc? One? What about inheritance? I hope you understand my logic. You also can find interesting topics with stop writing classes keywords.

At second in python module with functions really very similar to class (singleton) and methods.

At third it just less nested.

For grouping actions I try split controllers (views with flask notation, see https://stackoverflow.com/a/20999302/880326) by logic, but you can have more important criterias for this. Easy example:

views/
    __init__.py
    about.py
    home.py
    products.py
    user/
        __init__.py
        dashboard.py
        product_1.py
        product_2.py
        product_3.py
        settings.py

Upvotes: 5

Related Questions