Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think, the better way is to add the same view twice with difference renderers. Suppose we have the following view:</p> <pre><code>def my_view(request): return {"message": "Hello, world!"} </code></pre> <p>Now in our configuration we can add the same view twice:</p> <pre><code>from pyramid.config import Configurator config = Configurator() config.add_route('test', '/test', my_view, renderer="templates/my_template.mako") config.add_route('test', '/test', my_view, renderer="json", xhr=True) </code></pre> <p>What we have now:</p> <ol> <li>View <code>my_view</code> will render template <code>"templates/my_template.mako"</code> with returned dict provided as context if we will point our browser to url <code>/test</code>.</li> <li>If we will make XHR request with <code>my_view</code> will be called again, but now returned dict will be encoded as JSON and transmitted back to caller (please <a href="http://docs.pylonsproject.org/projects/pyramid/en/latest/api/config.html#pyramid.config.Configurator.add_view" rel="noreferrer">read docs</a> about checking if request was done via XHR).</li> </ol> <p>The same idea we can use for defining different routes but with the same view attached to them:</p> <pre><code>from pyramid.config import Configurator config = Configurator() config.add_route('test', '/test', my_view, renderer="templates/my_template.mako") config.add_route('test_json', '/test.json', my_view, renderer="json") </code></pre> <p>Now <code>/test</code> will trigger template rendering, but <code>/test.json</code> will return just JSON encoded string.</p> <p>You can go further and make dispatching to the right renderer via <code>accept</code> argument of <code>add_router</code> method:</p> <pre><code>from pyramid.config import Configurator config = Configurator() config.add_route('test', '/test', my_view, renderer="templates/my_template.mako") config.add_route('test', '/test', my_view, renderer="json", accept="application/json") </code></pre> <p>If request comes with header <code>Accept</code> set to <code>application/json</code> value JSON will be returned, otherwise you got rendered template.</p> <p>Note, this will work only if you have predefined set of data formats in which you want to encode responses from your views, but it's the usual case. In case you need dynamical dispatching you can decorate your views with <code>decorate</code> argument of <code>add_route</code> which will choose the right renderer with your rules.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload