zoukankan      html  css  js  c++  java
  • [python]pyramid 学习5 (render)

    输出json

    from pyramid.view import view_config

    @view_config(renderer='json')
    def hello_world(request):
    return {'content':'Hello!'}

    config.add_view('myproject.views.my_view', renderer='json')

    默认的render(string)

    from pyramid.response import Response
    from pyramid.view import view_config

    @view_config(renderer='string')
    def hello_world(request):
    return {'content':'Hello!'}

    输出

    {'content': 'Hello!'}

    JSONP Renderer

    __init__.py

    from pyramid.config import Configurator

    config = Configurator()
    config.add_renderer('jsonp', JSONP(param_name='callback'))

    views.py

    from pyramid.view import view_config

    @view_config(renderer='jsonp')
    def myview(request):
    return {'greeting':'Hello world'}

     

    Chameleon Template Renderers(*.pt *.txt)

    http://chameleon.repoze.org/

     

     config.add_view('myproject.views.hello_world',
    name='hello',
    context='myproject.resources.Hello',
    renderer='myproject:templates/foo.pt')

    config.add_view('myproject.views.hello_world',
    name='hello',
    context='myproject.resources.Hello',
    renderer='myproject:templates/foo.txt')


    Mako Template Renderer(*.mak *.mako)

    http://www.makotemplates.org/

     

     config.add_view('myproject.views.hello_world',
    name='hello',
    context='myproject.resources.Hello',
    renderer='foo.mak')

    config.add_view('myproject.views.hello_world',
    name='hello',
    context='myproject.resources.Hello',
    renderer='mypackage:templates/foo.mak')

     

    pyramid.response.Response 

    from pyramid.view import view_config

    @view_config(name='gone', renderer='templates/gone.pt')
    def myview(request):
    request.response.status = '404 Not Found'
    return {'URL':request.URL}

     

    Cookie

    def view(request):
    request.response.set_cookie('abc', '123')
    return request.response

     

    Header

    request.response
    response_content_type
    response_headerlist // [('Set-Cookie', 'abc=123'), ('X-My-Header', 'foo')].
    response_status
    response_charset
    response_cache_for











  • 相关阅读:
    AD9 如何画4层pcb板
    在Altium Designer 2009下如何添加Logo图
    [置顶] 整数拆分 整合算法
    altium designer 中的top/bottom solder和top/bottom paste mask
    vs2012 与 win7 不兼容的问题
    poj1742 Coins
    poj3181 Dollar Dayz
    poj1065 Wooden Sticks
    poj1631 Bridging signals
    poj3666 Making the Grade
  • 原文地址:https://www.cnblogs.com/bluefrog/p/2222156.html
Copyright © 2011-2022 走看看