I have a view-callable that is decorated with view_config to use a
mako template renderer.  This view-callable will be called both
directly by the browser and via XHR requests.  The following examples
illustrate my point.

Example A:
@view_config(renderer='json', xhr=True)
@view_config(renderer='template.mako', xhr=False)
def edit(self):
  template_vars = {}

  if self.request.is_xhr:
    return {'form': render('template-form.mako', render_vars)}

  return template_vars

Example B:
@view_config(renderer='template.mako')
def edit(self):
  template_vars = {}

  if self.request.is_xhr:
    return render_to_response('json', {'form':
render('template-form.mako', render_vars)})

  return template_vars

In Example A, any ajax requests will be returned a Response object
with a content_type of 'application/json.'  In Example B ajax will
receive a Response object from render_to_response with a content_type
set of 'text/html' instead of 'application/json' as would be expected.

Is this an issue with view_config?

I have dug in to the code in pyramid.renderers from the current trunk.
 The JSON renderer is being used for my request.  In
pyramid.renderers.JSON.__call__, lines 248-261, I see the response
object having its content_type set to application/json, but when the
response comes back to me from render_to_response, it is set to
text/html again.

Using pdb.set_trace():
> HOMEDIR/pyramid/pyramid/renderers.py(254)_render()
-> request = system.get('request')
(Pdb) l
249             """ Returns a plain JSON-encoded string with content-type
250             ``application/json``. The content-type may be overridden by
251             setting ``request.response.content_type``."""
252             def _render(value, system):
253                 import pdb; pdb.set_trace()
254  ->             request = system.get('request')
255                 if request is not None:
256                     response = request.response
257                     ct = response.content_type
258                     if ct == response.default_content_type:
259                         response.content_type = 'application/json'
(Pdb) n
> HOMEDIR/pyramid/pyramid/renderers.py(255)_render()
-> if request is not None:
(Pdb)
> HOMEDIR/pyramid/pyramid/renderers.py(256)_render()
-> response = request.response
(Pdb)
> HOMEDIR/pyramid/pyramid/renderers.py(257)_render()
-> ct = response.content_type
(Pdb) response.content_type
'text/html'
(Pdb) response.default_content_type
'text/html'
(Pdb) l
252             def _render(value, system):
253                 import pdb; pdb.set_trace()
254                 request = system.get('request')
255                 if request is not None:
256                     response = request.response
257  ->                 ct = response.content_type
258                     if ct == response.default_content_type:
259                         response.content_type = 'application/json'
260                 default = self._make_default(request)
261                 return self.serializer(value, default=default, **self.kw)
262     
(Pdb) n
> HOMEDIR/pyramid/pyramid/renderers.py(258)_render()
-> if ct == response.default_content_type:
(Pdb)
> HOMEDIR/pyramid/pyramid/renderers.py(259)_render()
-> response.content_type = 'application/json'
(Pdb)
> HOMEDIR/pyramid/pyramid/renderers.py(260)_render()
-> default = self._make_default(request)
(Pdb) response.content_type
'application/json'
(Pdb) r
--Return--
> HOMEDIR/pyramid/pyramid/renderers.py(261)_render()->'{"form": 
> "\\...ript>\\n\\n"}'
-> return self.serializer(value, default=default, **self.kw)
(Pdb)
> HOMEDIR/pyramid/pyramid/renderers.py(559)render()
-> return result
(Pdb)
--Return--
> HOMEDIR/pyramid/pyramid/renderers.py(559)render()->'{"form": 
> "\\...ript>\\n\\n"}'
-> return result
(Pdb)
> HOMEDIR/pyramid/pyramid/renderers.py(563)render_to_response()
-> return self._make_response(result, request)
(Pdb)
--Return--
> HOMEDIR/pyramid/pyramid/renderers.py(563)render_to_response()-><Respons...0 
> 200 OK>
-> return self._make_response(result, request)
(Pdb)
--Return--
> HOMEDIR/pyramid/pyramid/renderers.py(134)render_to_response()-><Respons...0 
> 200 OK>
-> return helper.render_to_response(value, None, request=request)
(Pdb)

** Returning a rendered response to my app **
> MY_APP/handlers/VIEW.py(185)edit()
-> response.content_type = 'application/json'
(Pdb) l
182             if self.request.is_xhr:
183                 response = render_to_response('json',
184                         {'form': render(self.form_template, template_vars)})
185  ->             response.content_type = 'application/json' <-
response.content_type is currently text/html
186                 return response
187     
188             return template_vars
189     
(Pdb) response.content_type
'text/html'


-- 
Ian Marcinkowski
[email protected]

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to