# /test{.format} no longer seems to work...
config.add_route('test', '/test.{ext}', view='ms.views.test')
视图.py:
from pyramid.response import Response
from pyramid.renderers import render
import json
def test(request):
extension = request.matchdict['ext']
variables = {'name' : 'blah', 'asd' : 'sdf'}
if extension == 'html':
output = render('mypackage:templates/blah.pt', variables, request=request)
if extension == 'json':
output = json.dumps(variables)
return Response(output)
有没有更简单的方法来做到这一点?使用 Pylons,这很简单:
def test(self, format='html'):
c.variables = {'a' : '1', 'b' : '2'}
if format == 'json':
return json.dumps(c.variables)
return render('/templates/blah.html')
我怀疑我以错误的方式接近这个......?