I have a page that I want to make accessible by either UI (for people) or
JSON (for robots). It seems easy to set up view_configs to change the
renderer appropriately via the Accept header, but the behavior needs to
change slightly depending on the renderer being used. For example, on
success, the UI needs to set a cookie and redirect, but the JSON needs to
return some data. On error, the UI will show a request.session.flash
notice, whereas the JSON should return an object describing the error. I'm
not sure how to achieve this. Here's a bit of code showing what I'm after:
@view_callable(context=context, name='page', renderer='form.pt')
@view_callable(context=context, name='page', accept='application/json',
request_method='POST', renderer='json')
def myform(context, request):
is_json = magic() # How would I do this?
if request.method == 'POST':
more_magic()
if is_json:
return {
'foo': 'bar',
}
else:
return HTTPFound('url')
return {
'form': 'stuff',
}
Obviously I could write a function to parse the Accept header, but it seems
silly to do that when Pyramid just did it for me with the "accept"
predicate.
Any ideas?
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.