问题
I've tried to build the simplest possible form in Sprox using pyramid.
# 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>
When I navigate to that view in the browser, I get
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>
回答1:
I have no idea what your real problem is, but I threw together a little demo to demonstrate how to setup Sprox under Pyramid. It was somewhat painful and uses some idioms that I consider pretty outdated, including requiring Paste, SQLAlchemy < 0.8, Genshi, and ToscaWidgets < 2.0. Hope this helps.
If I had to guess, you aren't adding the middleware to your app.
app = tw.api.make_middleware(
app,
{
'toscawidgets.framework' : 'wsgi',
'toscawidgets.middleware.inject_resources' : True,
},
stack_registry=True,
)
https://github.com/mmerickel/pyramid_sprox_demo
来源:https://stackoverflow.com/questions/14425584/hello-world-of-sprox-with-pyramid-part-2