[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











posted on 2011-10-23 23:34  bluefrog  阅读(2519)  评论(0编辑  收藏  举报