Calling a view without a view-config or add-view

37 views
Skip to first unread message

Zak

unread,
Apr 3, 2012, 7:52:51 AM4/3/12
to pylons-...@googlegroups.com
I want to map a route to a class-based view without needing to name the class again in a view-config decorator and without needing to explicitly name it in a config.add_view in the __init__. 

I want 

config.add_route('MyClass', '/')

to map that route to 

@view_defaults(renderer='string')
class MyClass(object):
    def __init__(self, request):
        self.request = request

    def __call__(self):
        return 'a string'

without needing any extra code. Is this possible?

Michael Merickel

unread,
Apr 3, 2012, 9:30:52 AM4/3/12
to pylons-...@googlegroups.com
Well you can certainly do it yourself with the "add_my_route" function that you create that calls both for you... Otherwise, no, it's by design in Pyramid that routes and views are separate concepts.

You can always use traversal, and then it's just a single add_view call with no add_route. :-)

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-discuss/-/LSh9cGEFUJkJ.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.

Zak

unread,
Apr 3, 2012, 3:23:52 PM4/3/12
to pylons-...@googlegroups.com
I see. I guess it just seemed odd to me not to have a default behavior where a route would be matched automatically to the appropriately named class/function, but I guess that makes Pyramid a more flexible platform. Thank you.


On Tuesday, April 3, 2012 8:30:52 AM UTC-5, Michael Merickel wrote:
Well you can certainly do it yourself with the "add_my_route" function that you create that calls both for you... Otherwise, no, it's by design in Pyramid that routes and views are separate concepts.

You can always use traversal, and then it's just a single add_view call with no add_route. :-)

On Tue, Apr 3, 2012 at 6:52 AM, Zak wrote:
I want to map a route to a class-based view without needing to name the class again in a view-config decorator and without needing to explicitly name it in a config.add_view in the __init__. 

I want 

config.add_route('MyClass', '/')

to map that route to 

@view_defaults(renderer='string')
class MyClass(object):
    def __init__(self, request):
        self.request = request

    def __call__(self):
        return 'a string'

without needing any extra code. Is this possible?

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-discuss/-/LSh9cGEFUJkJ.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to pylons-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages