Of course, after posting this I immediately find all the information I'm looking for.
It looks like request.accept is more powerful than a simple string: http://webob.readthedocs.org/en/latest/reference.html#accept-headers However, I'll need to be much more careful with my use of the accept predicate, seeing that most browsers include "*/*" in their Accept headers. https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation#Default_values I'm thinking that maybe for simplicity's sake I'm better off using file extensions for my content type, i.e.: @view_callable(context=context, name='page', renderer='form.pt') @view_callable(context=context, name='page.json', request_method='POST', renderer='json') def myform(context, request): is_json = request.view_name.endswith('.json') ... Thoughts on the matter? On Saturday, February 7, 2015 at 10:40:47 AM UTC-8, Theron Luhn wrote: > > 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.
