How to enable JSON renderer as default for all views?

63 views
Skip to first unread message

jens.t...@gmail.com

unread,
Nov 1, 2017, 4:23:04 AM11/1/17
to pylons-discuss
Hello,

The documentation for the default renderer for all views seems to suggest that the following should work:

from pyramid.renderers import JSON

config = Configurator(
    …
    )
config.add_renderer(None, JSON())

Alas, when one of the views returns HTTPOk() the response type is still html. How to I make JSON the default renderer here for all views?

Thanks!
Jens

Michael Merickel

unread,
Nov 1, 2017, 11:48:02 AM11/1/17
to Pylons
This renderer feature does not override the other one which is "if you return a Response object then no renderers will be invoked". HTTPOk is a Response subclass. This is covered in the first few paragraphs of the renderer chapter. I'd be open to a PR that clarified the docs on this in the section you linked since it does say "all views". It is infact "all views that do not return something directly adaptable to a response such as an implementer of IResponse, or an object whose type is registered as a response adapter".

--
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 pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/d730910a-2425-48fa-841b-4f891ddc525e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jens.t...@gmail.com

unread,
Nov 6, 2017, 6:34:29 PM11/6/17
to pylons-discuss
Thanks Michael!

Would you like me to submit a PR for the adjusted comment as per your suggestion?

All of my (Cornice) view functions are decorated like so:

@some_service.post(                                                                         
    content_type="application/json",                                                                
    accept="application/json",                                                                      
    …
    )

which I think asks for the json renderer for responses. Unless, if I understand you correctly, the returned object is response object (instead of a dict or None or whatever).

So should I then use

def some_view_fun(request):
    …
    request.response.status = 201 # Created
    return None

instead of returning a HTTPCreated() here? 

Thank you!
Jens

Michael Merickel

unread,
Nov 6, 2017, 6:51:50 PM11/6/17
to Pylons
Jens, I'm happy to review a PR. Also you are correct that if you return `None` then the json renderer will kick in and use request.response. As far as how this works with certain responses such as 201, 202, etc you will probably want to be careful and return an explicit Response object in some cases where you do not want an actual response body. For example, returning `None` will result in a JSON null being rendered as the application/json response body.

--
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 pylons-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages