1

我尝试使用金字塔在Sprox中构建最简单的形式。

# model.py
class Allocation:
    # some fields ...
class SproxForm(AddRecordForm):
    __model__ = Allocation

sprox_form = SproxForm(DBSession)

# views.py
@view_config(route_name='sprox_add', renderer='templates/sprox.pt')
def sprox_form_add(request):
    return {'form':sprox_form,'form_input':{}}

<!-- sprox.pt -->
<html>
<body>
<div tal:content="structure form(form_input)"/>
</body>
</html>

当我在浏览器中导航到该视图时,我得到

Traceback (most recent call last):
  File "D:\prg\py2\lib\site-packages\pyramid_debugtoolbar-1.0.4-py2.7.egg\pyramid_debugtoolbar\panels\performance.py", line 69, in noresource_timer_handler
    result = handler(request)
  File "D:\prg\py2\lib\site-packages\pyramid\tweens.py", line 21, in excview_tween
    response = handler(request)
  File "D:\prg\py2\lib\site-packages\pyramid_tm-0.7-py2.7.egg\pyramid_tm\__init__.py", line 82, in tm_tween
    reraise(*exc_info)
  File "D:\prg\py2\lib\site-packages\pyramid_tm-0.7-py2.7.egg\pyramid_tm\__init__.py", line 63, in tm_tween
    response = handler(request)
  File "D:\prg\py2\lib\site-packages\pyramid\router.py", line 161, in handle_request
    response = view_callable(context, request)
  File "D:\prg\py2\lib\site-packages\pyramid\config\views.py", line 367, in rendered_view
    context)
  File "D:\prg\py2\lib\site-packages\pyramid\renderers.py", line 531, in render_view
    return self.render_to_response(response, system, request=request)
  File "D:\prg\py2\lib\site-packages\pyramid\renderers.py", line 561, in render_to_response
    result = self.render(value, system_values, request=request)
  File "D:\prg\py2\lib\site-packages\pyramid\renderers.py", line 557, in render
    result = renderer(value, system_values)
  File "D:\prg\py2\lib\site-packages\pyramid\chameleon_zpt.py", line 42, in __call__
    result = self.template(**system)
  File "D:\prg\py2\lib\site-packages\chameleon\template.py", line 125, in __call__
    return self.render(**kwargs)
  File "D:\prg\py2\lib\site-packages\chameleon\zpt\template.py", line 257, in render
    return super(PageTemplate, self).render(**vars)
  File "D:\prg\py2\lib\site-packages\chameleon\template.py", line 190, in render
    raise_with_traceback(exc, tb)
  File "D:\prg\py2\lib\site-packages\chameleon\template.py", line 172, in render
    self._render(stream, econtext, rcontext)
  File "sprox_3aacd319285f07641f9292cdee192301f3a0bccd.py", line 136, in render

  File "D:\prg\py2\lib\site-packages\chameleon\zpt\template.py", line 261, in include
    self._render(*args, **kwargs)
  File "template_f352de36cd7e76f81188f12078004a91a152cf44.py", line 376, in render

  File "sprox_3aacd319285f07641f9292cdee192301f3a0bccd.py", line 106, in __fill_content

  File "D:\prg\py2\lib\site-packages\sprox\viewbase.py", line 113, in __call__
    return self.display(*args, **kw)
  File "D:\prg\py2\lib\site-packages\sprox\viewbase.py", line 119, in display
    return self.__widget__.display(*args, **kw)
  File "D:\prg\py2\lib\site-packages\tw2\core\widgets.py", line 438, in display
    return self.generate_output(displays_on)
  File "D:\prg\py2\lib\site-packages\tw2\core\widgets.py", line 475, in generate_output
    mw,
  File "D:\prg\py2\lib\site-packages\tw2\core\templating.py", line 204, in render
    return callback(kwargs)
  File "D:\prg\py2\lib\site-packages\tw2\core\templating.py", line 144, in <lambda>
    return lambda kwargs: Markup(tmpl.render(**kwargs))
  File "D:\prg\py2\lib\site-packages\mako\template.py", line 412, in render
    return runtime._render(self, self.callable_, args, data)
  File "D:\prg\py2\lib\site-packages\mako\runtime.py", line 766, in _render
    **_kwargs_for_callable(callable_, data))
  File "D:\prg\py2\lib\site-packages\mako\runtime.py", line 798, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "D:\prg\py2\lib\site-packages\mako\runtime.py", line 824, in _exec_template
    callable_(context, *args, **kwargs)
  File "form_mak", line 49, in render_body

  File "D:\prg\py2\lib\site-packages\tw2\core\widgets.py", line 436, in display
    r.prepare()
  File "D:\prg\py2\lib\site-packages\tw2\core\resources.py", line 165, in prepare
    resources = rl['middleware'].resources
KeyError: 'middleware'

 - Expression: "form(form_input)"
 - Filename:   d:\prg\py2\awsalloc2\awsalloc2\templates\sprox.pt
 - Location:   (9:28)
 - Source:     ... v tal:content="structure form(form_input)" />
                                            ^^^^^^^^^^^^^^^^
 - Arguments:  repeat: {...} (0)
               renderer_name: templates/sprox.pt
               req: <Request - at 0x46fceb8L>
               form: <SproxForm - at 0x471cb38L>
               request: <Request - at 0x46fceb8L>
               renderer_info: <RendererHelper - at 0x4a3b7b8L>
               context: <instance None at 0x4f93c88L>
               form_input: {...} (0)
               view: <function sprox_form_add at 0x4a3d278L>
4

1 回答 1

2

我不知道你真正的问题是什么,但我整理了一个小演示来演示如何在 Pyramid 下设置 Sprox。这有点痛苦,并且使用了一些我认为已经过时的习语,包括要求 Paste、SQLAlchemy < 0.8、Genshi 和 ToscaWidgets < 2.0。希望这可以帮助。

如果我不得不猜测,您并没有将中间件添加到您的应用程序中。

app = tw.api.make_middleware(
    app,
    {
        'toscawidgets.framework' : 'wsgi',
        'toscawidgets.middleware.inject_resources' : True,
    },
    stack_registry=True,
)

https://github.com/mmerickel/pyramid_sprox_demo

于 2013-01-20T21:40:22.160 回答