I know I'm late to the party, but found this post while trying to achieve 
the same thing. We've found a simpler way, taking advantage of the fact 
that Response subclasses WebOb which (since 2012) has a 'json' kwarg 
(aliased to 'json_body'). So we can do this, short-n-sweet:

@view_config(route_name='jsonplain', renderer='json', request_method='GET')
def jsonplain(request):
    return HTTPOk(json={'message': 'Could not process entity'})




On Monday, March 31, 2014 at 11:41:03 AM UTC-4, Tres Seaver wrote:
>
> -----BEGIN PGP SIGNED MESSAGE----- 
> Hash: SHA1 
>
> On 03/31/2014 05:11 AM, Chung WONG wrote: 
> > For example, there is a view returns a http exception to a request: 
> > @view_config(route_name='route', renderer='json', 
> > request_method='GET') def view(request): *        return 
> > HTTPUnprocessableEntity(detail='details', comment='comment', 
> > body_template='body_template')* 
> > 
> > it returned a full html page *<html>* *<head>* *<title>422 
> > Unprocessable Entity</title>* *</head>* *<body>* *<h1>422 
> > Unprocessable Entity</h1>** body_template* *</body>* *</html>* is it 
> > possible to make the return as a JSON so that the frontend can extract 
> > the error message easily? 
>
> When you return a response object, pyramid bypasses the renderer.  In your 
> case, you would need something like:: 
>
>     import json 
>
>     @view_config(route_name='route', renderer='json', 
> request_method='GET') 
>     def view(request): 
>         error = {'message': 'Could not process entity'} 
>         return HTTPUnprocessableEntity( 
>                 body=json.dumps(error), 
>                 content_type='application/json') 
>
>
> Tres. 
> - -- 
> =================================================================== 
> Tres Seaver          +1 540-429-0999          [email protected] 
> <javascript:> 
> Palladion Software   "Excellence by Design"    http://palladion.com 
> -----BEGIN PGP SIGNATURE----- 
> Version: GnuPG v1.4.11 (GNU/Linux) 
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ 
>
> iEYEARECAAYFAlM5jIkACgkQ+gerLs4ltQ7vXQCghToWo9ssPCoJrh4hZPjKi3uG 
> 3eUAoM5ixHv6jt0p5JdvcxmZRGJcpIEd 
> =khsO 
> -----END PGP SIGNATURE----- 
>
>

-- 
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.

Reply via email to