On Fri, Jun 15, 2012 at 9:13 AM, pylover2000 <[email protected]> wrote:

> Hello everyone!
>
> *Background:* Not new to coding, relatively new to Python. Experience
> with C# and PHP frameworks for web-app development. New to Python
> frameworks. I think Pyramid is the way to go.
>
> *My question:* *- [did my research, could not find what I am looking for,
> probably because of my not-so-great English]*
> *
> *
> *+ *I would like to have a *central (routing) file* where all the
> routings are defined. e.g. route / to ... route /{method}/{value} to .....
> (.... being different view callable(s))
> *+ AND *I would like to have each and every one of my view callables *NOT
> to exist in a single .py* but to have *their own .py file* (to have
> better file structure and readability)
>
> How do I link the two?
>

Both of these are common in larger Pyramid applications, although usually
you would group the views by application section rather than than making
every view separate.

The first thing to note is that functions and classes are not related to
modules at all.  A .py file is a module. It can contain any number of
functions and classes.

For the routes, make a module, normally called 'routes.py' at the same
level as the top-level '__init__.py'. Put a 'def includeme(config):'
function in, it, containing all the config.add_route() calls. Then in the
main function, include it with 'config.include(".routes")' .

For the views, convert views.py to a package, meaning replace it with a
'views' directory with an empty '__init__.py' file, and put your view
modules in there. The view callables will still have the same structure and
imports as normal.

To connect the views to routes, use '@view_config' the normal way, with a
corresponding 'config.scan(".views")' call in the main function.

To use the same route with multiple views, using a match variable or some
other aspect of the request to determine which view to call, see the
'@view_config' optional arguments.

What you CAN"T do in Pyramid (unlike Pylons), is use a routing variable to
look up a view by name. Every view must be attached to a route at startup,
using the '@view_config' arguments as the link between them.




>
> *also:*
> *+ *How can I create a class .py which is not intended to be used as view
> callable but as an object *to be called by view callables*, again, *very
> importantly*, *on its own .py file. *
>
>
You can define any utility classes you want, and put them anywhere. If the
classes are more or less generic, and applicable to several views, you'd
normally create a 'lib' package and put your utility modules there.

I wrote some documentation which was intended for people with a Pylons
background, but it may be indirectly useful. See:

http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/pylons/index.html
Pyramid for Pylons Users

-- 
Mike Orr <[email protected]>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to