Question

What are the Pyramid / Python equivalents of Model - View - Controller of PHP Frameworks such as Kohana?

In Pyramid "Model" is .... and it is used for .....
In Pyramid "View" is .... and it is used for .....
In Pyramid "Controller" is .... and it is used for .....

I am trying to understand Pyramid's logic. As an addition to the answer, any help, documentation etc would be appreciated.

Thanks.

Was it helpful?

Solution

Pylons, one of the two frameworks that joined together to be Pyramid ( the other was repoze.bfg ) was "close" to an MVC system.

I put close in quotations, because over the past few years a lot of people have been fighting about what MVC means... and many projects that once touted themselves as "MVC" started to call them "MTC" (model template controller) "MT" (model template) or "MV" (model view). Everyone agrees on what the "model" is, but exactly what the "view" and "controller" map to - on a given framework - can be a point of contention.

Pyramid and pylons both have a "dispatcher" functionality to set up the mapping for a request. Under pylons its in config/routes.py ; under Pyramid it's a little different -- the default scaffolds have the routing in app/init.py , but you're free to break it out into app/routes.py or use config.include() to push it into you 'handlers' or config.scan() to pull it from your 'views'.

'handlers' in pyramid are provided by pyramid_handlers, and are really just 'views' with a bunch of auto-generation stuff in there. If you wanted to, your apps could use both handlers AND views ( mine do ).

In any event, depending on how you interpret MVC / MTC / etc , this is a loose table of what you might want:

           || mvt            | mvc            | mvc
==========================================================================
model      || sqlalchemy     | sqlalchemy     | sqlalchemy
view       || views/handlers | templates      | views/handlers + templates
controller ||                | views/handlers | dispatch/routing
template   || templates      |                |

Quick note- I'm defining the above not based on my interpretation or what the 'official' MVC definition is... It's based in relation to how other popular frameworks market themselves.

OTHER TIPS

If you want, with pyramid you can simulate the MVC pattern:

  • Model: For example using sqlalchemy (http://docs.sqlalchemy.org)
  • View: Using templates and view methods.
  • Controller: You can use the package pyramid_handlers, to create controllers and map actions defined in a route to actions in the controller, for example:
   Class HomeController(object):
     def __init__(self, request):
          self.request = request

      def form_proc(self):
          name = self.request.params['name']
          ... bla, bla, bla ...

In the config you can add something like:

    config.add_handler('home', '/home/{action}',
                       handler='mypackage.HomeController')

If you put this url in your form action -> http://SERVER_NAME/home/form_proc, you can process the form.

Pyramid give you all the flexibility if you need it.

From the Pyramid Introduction:

You Say Pyramid is MVC, But Where’s The Controller?

The Pyramid authors believe that the MVC pattern just doesn’t really fit the web very well. In a Pyramid application, there is a resource tree, which represents the site structure, and views, which tend to present the data stored in the resource tree and a user-defined “domain model”. However, no facility provided by the framework actually necessarily maps to the concept of a “controller” or “model”. So if you had to give it some acronym, I guess you’d say Pyramid is actually an “RV” framework rather than an “MVC” framework. “MVC”, however, is close enough as a general classification moniker for purposes of comparison with other web frameworks.

I have experience with CakePHP and now I'm starting with Pyramid and Python. There is not a direct mapping but it is not because pyramid does things in weird manner but because framework authors abused the MVC term.

In Cake, for example, there are some classes that they like to call 'Models' but most of the time are just ORM classes. The controllers are mostly used as namespaces for related methods called 'actions' that pass the data to the views, which are just the templates.

In pyramid terms, 'Resources' are the 'models', and you are free to use wherever you want here, if you want an ORM you can use SQLAlchemy for example or mongodb or wherever.

The framework itself functions as the 'controllers', and the actions are called 'views' this can be normal functions or classes, you are free to organize them wherever you like. This views may use a template and a renderer to construct the response that is send to the browser.

Hope it helps (please excuse my bad english)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top