Distinguishing between view names

22 views
Skip to first unread message

Theron Luhn

unread,
Jun 15, 2012, 3:11:22 PM6/15/12
to pylons-...@googlegroups.com
A lot of times I use the same view for both editing items and making new items.  How can I determine which view name is being called?  I know I'm not explaining myself very well, so to talk in code:

@view_config(context=Context, name='new', renderer='edit.pt')
@view_config(context=Context, name='edit', renderer='edit.pt')
def edit(context, request):
    if [name is 'edit']:
        #Load data to edit
    elif [name is 'new']:
        #Do something else
    #Do more stuff

Andreas Kaiser

unread,
Jun 15, 2012, 3:35:26 PM6/15/12
to pylons-...@googlegroups.com
I prefer a class based approach for things like this. It's a little more code, but IMHO has much better readability:

@view_defaults(context=Context, renderer='edit.pt')
class FooViews():
def __init__(self, context, request):
self.context = context
self.request = request

def do_common_stuff(self):
# do more stuff

@view_config(name='new')
def new(self):
#Do something else
self.do_common_stuff()

@view_config(name='edit')
#Load data to edit
self.do_common_stuff()


HTH,

Andreas

Jason

unread,
Jun 15, 2012, 3:38:37 PM6/15/12
to pylons-...@googlegroups.com
I believe it is available in request.matchdict['action'] -- at least if you are using urldispatch.

-- jason 

Jeff Dairiki

unread,
Jun 15, 2012, 4:25:13 PM6/15/12
to pylons-...@googlegroups.com
Or, if you're using traversal (which looks to be the case in your
example), the view name is in ``request.view_name``.

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/api/request.html#pyramid.request.Request.view_name

Theron Luhn

unread,
Jun 15, 2012, 7:11:36 PM6/15/12
to pylons-...@googlegroups.com
Thanks Jeff, exactly what I was looking for!

- Theron



--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
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.


Reply all
Reply to author
Forward
0 new messages