Re: Changing content-type on renderer response

221 views
Skip to first unread message

Chris McDonough

unread,
Nov 5, 2012, 7:09:34 PM11/5/12
to pylons-...@googlegroups.com
On 11/05/2012 06:38 PM, Marten wrote:
> Hello,
>
> I'm working with Jinja2 templates but the response always renders as
> text/html.
>
> How can I change it to text/plain or text/xml for example on a specific
> view? I don't want to write a new renderer for that. I know that I can
> achieve this very cumbersome by getting the renderer, creating a
> response object and defining content_type and body attributes then.
>
> Is there a simple way to do this, like specifying a
> response_content_type attribute in the view_config?

@view_config(renderer='foo.jinja2', ...)
def aview(request):
request.response.content_type = 'text/xml'
return {'a':1}

Marten

unread,
Nov 6, 2012, 12:09:59 PM11/6/12
to pylons-...@googlegroups.com
Thanks! I thought accessing the request.response object would automatically enforce to send a manual response, but it works fine the way you suggested it.

Tjelvar

unread,
Mar 27, 2013, 5:22:36 AM3/27/13
to pylons-...@googlegroups.com
Any suggestions on how one can modify the content-type of a response when using multiple @view_config decorators on the same view?

E.g.

@view_config(accept='text/html', renderer='html.pt', ...)
@view_config(accept='text/xml', renderer='xml.pt', ...)
@view_config(accept='application/json' renderer='json')
def aview(request):
     return {'a':1]


Jason

unread,
Mar 27, 2013, 9:00:43 AM3/27/13
to pylons-...@googlegroups.com


On Wednesday, March 27, 2013 5:22:36 AM UTC-4, Tjelvar wrote:
Any suggestions on how one can modify the content-type of a response when using multiple @view_config decorators on the same view?

E.g.

@view_config(accept='text/html', renderer='html.pt', ...)
@view_config(accept='text/xml', renderer='xml.pt', ...)
@view_config(accept='application/json' renderer='json')
def aview(request):
     return {'a':1]


Hi Tjelvar,
Would you be able to use an event subscriber (BeforeRender?) that would change the content-type of the response? 
I do this for an application, but I am using a matchdict parameter to determine the response content-type in the subscriber.

--
Jason

Joel Kaiser

unread,
Mar 27, 2013, 9:29:44 AM3/27/13
to pylons-...@googlegroups.com
Hi,

I don't know if its a nice solution, but you can also set the content-type in your page template:

<tal:setHeader >
    ${setattr(request.response,'content_type','text/xml')}
</tal:setHeader>

Joel


2013/3/27 Jason <ja...@deadtreepages.com>

--
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-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tjelvar

unread,
Mar 27, 2013, 12:56:44 PM3/27/13
to pylons-...@googlegroups.com, kaise...@googlemail.com
Jason and Joel - thank you for your suggestions.

I managed to get it to work using the suggestion below.

Michael Merickel

unread,
Mar 27, 2013, 2:02:04 PM3/27/13
to Pylons
Renderers are typically responsible for setting the content type on the request.response object themselves (if you haven't overridden it already). It'd be possible to write a view predicate that you can use next to the renderer argument, to make it more declarative.

class ResponseContentTypePredicate(object):
    def __init__(self, config, val):
        self.val = val

    def text(self):
        return 'response content-type = %s' % self.val
    phash = text

    def __call__(self, context, request):
        if self.val is not None:
            request.response.content_type = self.val
        return True

config.add_view_predicate('response_content_type', ResponseContentTypePredicate)

@view_config(..., renderer='foo.mako', response_content_type='text/xml')
def ...

Anyway, just another option for you. This was written without the docs so expect an error somewhere!

uralbash

unread,
Mar 28, 2013, 12:34:15 AM3/28/13
to pylons-...@googlegroups.com
#view.py
from pyramid.events import subscriber
from pyramid.events import NewRequest
 
@subscriber(NewRequest)
def add_header(event):
    response = event.request.response
    response.headers.add('Content-type', 'text/html')


вторник, 6 ноября 2012 г., 5:38:55 UTC+6 пользователь Marten написал:
Hello,

I'm working with Jinja2 templates but the response always renders as text/html.

How can I change it to text/plain or text/xml for example on a specific view? I don't want to write a new renderer for that. I know that I can achieve this very cumbersome by getting the renderer, creating a response object and defining content_type and body attributes then.

Is there a simple way to do this, like specifying a response_content_type attribute in the view_config?

Kind regards
Marten

Chris McDonough

unread,
Mar 28, 2013, 1:27:33 AM3/28/13
to pylons-...@googlegroups.com
On Wed, 2013-03-27 at 21:34 -0700, uralbash wrote:
> #view.py
> from pyramid.events import subscriber
> from pyramid.events import NewRequest
>
> @subscriber(NewRequest)
> def add_header(event):
> response = event.request.response
> response.headers.add('Content-type', 'text/html')

Not a good idea if it's only for one view. Better:

@view_config(renderer='foo.jinja2')
def aview(request):
request.response.content_type = 'text/xml'
return {'a':1}


>
>
> вторник, 6 ноября 2012 г., 5:38:55 UTC+6 пользователь Marten написал:
> Hello,
>
>
> I'm working with Jinja2 templates but the response always
> renders as text/html.
>
>
> How can I change it to text/plain or text/xml for example on a
> specific view? I don't want to write a new renderer for that.
> I know that I can achieve this very cumbersome by getting the
> renderer, creating a response object and defining content_type
> and body attributes then.
>
>
> Is there a simple way to do this, like specifying a
> response_content_type attribute in the view_config?
>
>
> Kind regards
> Marten
>

Tjelvar

unread,
Mar 28, 2013, 5:54:27 AM3/28/13
to pylons-...@googlegroups.com
Thank you Michael this works beautifully.
Reply all
Reply to author
Forward
0 new messages