目标:我尝试将Mongo DB与Pyramid 1.1基本应用程序集成。
背景:使用基本命令通过本书(https://docs.pylonsproject.org/projects/pyramid/1.1/narr/project.html#creating-the-project)创建应用程序“paste create -t pyramid_starter”
我按照这本食谱文章:https://docs.pylonsproject.org/projects/pyramid_cookbook/dev/mongo.html
问题:似乎当我将MongoDB连接添加到请求中时,我得到了“内部服务器错误”
我已经尝试了几篇文章,似乎我必须更多地启动调试系统? 有没有人为此找到了简单的解决方案?
如果它有助于某些专家
,则为例外Exception happened during processing of request from ('127.0.0.1', 53697)
Traceback (most recent call last):
File "virtualenv\lib\site-packages\paste-1.7.5.1-py2.7.egg\paste\httpserver.py", line 1068, in process_request_in_thread
self.finish_request(request, client_address)
File "C:\Python27\Lib\SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\Lib\SocketServer.py", line 639, in __init__
self.handle()
File "virtualenv\lib\site-packages\paste-1.7.5.1-py2.7.egg\paste\httpserver.py", line 442, in handle
BaseHTTPRequestHandler.handle(self)
File "C:\Python27\Lib\BaseHTTPServer.py", line 343, in handle
self.handle_one_request()
...
File "C:\Python27\lib\site-packages\pyramid_debugtoolbar-0.8-py2.7.egg\pyramid_debugtoolbar\panels\__init__.py", line 24, in render
return render(template_name, vars, request=request)
File "virtualenv\lib\site-packages\pyramid-1.2a1-py2.7.egg\pyramid\renderers.py", line 69, in render
return helper.render(value, None, request=request)
File "virtualenv\lib\site-packages\pyramid-1.2a1-py2.7.egg\pyramid\renderers.py", line 418, in render
result = renderer(value, system_values)
File "C:\Python27\lib\site-packages\pyramid_jinja2-1.1-py2.7.egg\pyramid_jinja2\__init__.py", line 277, in __call__
return self.template.render(system)
File "C:\Python27\lib\site-packages\jinja2-2.6-py2.7.egg\jinja2\environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "C:\Python27\lib\site-packages\pyramid_debugtoolbar-0.8-py2.7.egg\pyramid_debugtoolbar\panels\templates\request_vars.jinja2", line 110, in top-level template code
<td>{{ value|escape }}</td>
File "virtualenv\lib\site-packages\markupsafe-0.15-py2.7.egg\markupsafe\_native.py", line 20, in escape
return s.__html__()
File "virtualenv\lib\site-packages\pymongo-2.0.1-py2.7-win-amd64.egg\pymongo\collection.py", line 1156, in __call__
self.__name)
TypeError: 'Collection' object is not callable. If you meant to call the '__html__' method on a 'Database' object it is failing because no such method exists.
答案 0 :(得分:2)
另一种可能的解决方案是使用配置文件中的'debugtoolbar.panels'设置来禁用request_vars面板(导致问题的原因):
[app:main]
.. other stuff ...
debugtoolbar.panels =
pyramid_debugtoolbar.panels.versions.VersionDebugPanel
pyramid_debugtoolbar.panels.settings.SettingsDebugPanel
pyramid_debugtoolbar.panels.headers.HeaderDebugPanel
# pyramid_debugtoolbar.panels.request_vars.RequestVarsDebugPanel
pyramid_debugtoolbar.panels.renderings.RenderingsDebugPanel
pyramid_debugtoolbar.panels.logger.LoggingPanel
pyramid_debugtoolbar.panels.performance.PerformanceDebugPanel
pyramid_debugtoolbar.panels.routes.RoutesDebugPanel
pyramid_debugtoolbar.panels.sqla.SQLADebugPanel
答案 1 :(得分:0)
该错误意味着您尝试调用数据库实例中不存在的方法( html )。
>>> conn = Connection()
>>> db = conn.mydb
>>> col = db.mycoll
>>> col = db.mycoll()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/virtualenvs/myenv/lib/python2.7/site-packages/pymongo-2.0-py2.7-macosx-10.6-x86_64.egg/pymongo/collection.py", line 1156, in __call__
self.__name)
TypeError: 'Collection' object is not callable. If you meant to call the 'mycoll' method on a 'Database' object it is failing because no such method exists.
如果您尚未修改代码,则可能是标记尝试在数据库实例中调用 html ()的错误
s.__html__()
答案 2 :(得分:0)
Pymongo的Database
和Collection
个对象响应__getattr__
以提供更好的界面,让您编写如下代码:
db.foo.bar.find(...)
对__getattr__
的任何调用都会成功,但不幸的是,这会混淆一些希望某些属性可以调用的库(Pymongo Collection
对象不可调用,除了提出你在上面看到的异常)。
我在Pyramid项目中所做的只是使用资源中的数据库,以防止对数据库或集合的引用出现在视图或其他代码中的模块级别。作为一个额外的好处,这最终成为一种强制关注点分离的好方法,以便资源处理数据库操作,而视图只转换它以便在模板中显示。