Python 如何集成Pyramid 1.1和Mongo DB-尽可能少的线
目标:我尝试将Mongo DB与Pyramid 1.1基本应用程序集成 背景:应用程序由本书创建(https://docs.pylonsproject.org/projects/pyramid/1.1/narr/project.html#creating-项目)使用基本命令“粘贴创建-t棱锥体\u启动器” 我看了这篇食谱文章: 问题:似乎每当我将MongoDB连接添加到请求中时,都会遇到“内部服务器错误” 我已经尝试了几篇文章,似乎我必须开始调试系统更多? 有人找到了简单的解决办法吗 例外,如果它有助于一些专家Python 如何集成Pyramid 1.1和Mongo DB-尽可能少的线,python,mongodb,pyramid,Python,Mongodb,Pyramid,目标:我尝试将Mongo DB与Pyramid 1.1基本应用程序集成 背景:应用程序由本书创建(https://docs.pylonsproject.org/projects/pyramid/1.1/narr/project.html#creating-项目)使用基本命令“粘贴创建-t棱锥体\u启动器” 我看了这篇食谱文章: 问题:似乎每当我将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.
处理来自('127.0.0.1',53697'的请求时发生异常
回溯(最近一次呼叫最后一次):
文件“virtualenv\lib\site packages\paste-1.7.5.1-py2.7.egg\paste\httpserver.py”,第1068行,进程中\u请求\u线程
自我完成请求(请求、客户地址)
文件“C:\Python27\Lib\SocketServer.py”,第323行,在finish\u请求中
self.RequestHandlerClass(请求、客户端地址、self)
文件“C:\Python27\Lib\SocketServer.py”,第639行,在_init中__
self.handle()
文件“virtualenv\lib\site packages\paste-1.7.5.1-py2.7.egg\paste\httpserver.py”,第442行,在句柄中
BaseHTTPRequestHandler.handle(self)
文件“C:\Python27\Lib\BaseHTTPServer.py”,第343行,位于句柄中
self.handle_one_请求()
...
文件“C:\Python27\lib\site packages\pyramid\u debugtoolbar-0.8-py2.7.egg\pyramid\u debugtoolbar\panels\\uuuuuu init\uuuuu.py”,第24行,在渲染中
返回渲染(模板名称,变量,请求=请求)
文件“virtualenv\lib\site packages\pyramid-1.2a1-py2.7.egg\pyramid\renderers.py”,第69行,在渲染中
return helper.render(值,无,请求=请求)
文件“virtualenv\lib\site packages\pyramid-1.2a1-py2.7.egg\pyramid\renderers.py”,第418行,在渲染中
结果=渲染器(值、系统值)
文件“C:\Python27\lib\site packages\pyramid\u jinja2-1.1-py2.7.egg\pyramid\u jinja2\\uuuuuuuuuuu init\uuuuuuuuuu.py”,第277行,在调用中__
返回self.template.render(系统)
文件“C:\Python27\lib\site packages\jinja2-2.6-py2.7.egg\jinja2\environment.py”,第894行,在render中
返回self.environment.handle\u异常(exc\u info,True)
顶级模板代码中的文件“C:\Python27\lib\site packages\pyramid\u debugtoolbar-0.8-py2.7.egg\pyramid\u debugtoolbar\panels\templates\request\u vars.jinja2”,第110行
{{value | escape}
文件“virtualenv\lib\site packages\markupsafe-0.15-py2.7.egg\markupsafe\\ u native.py”,第20行,在escape中
返回s.\uuuuHTML\uuuuuuu()
文件“virtualenv\lib\site packages\pymongo-2.0.1-py2.7-win-amd64.egg\pymongo\collection.py”,第1156行,在调用中__
自我(姓名)
TypeError:“集合”对象不可调用。如果您想在“数据库”对象上调用“\uuuuhtml\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。
该错误意味着您试图调用数据库实例中不存在的方法(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.
Pymongo的
数据库
和集合
对象响应\uuu getattr\uuuu
,以提供更好的界面,并允许您编写如下代码:
db.foo.bar.find(...)
任何对\uuuu getattr\uuuu
的调用都会成功,但不幸的是,这会混淆一些期望某些属性可调用的库(Pymongo集合
对象不可调用,除非引发上面看到的异常)
我在金字塔项目中所做的只是从资源中使用数据库,以防止对数据库或集合的引用出现在视图或其他代码的模块级中。作为一个附加的好处,这最终是一种很好的强制分离关注点的方法,这样资源就可以处理数据库操作,视图只转换这些操作以在模板中显示。另一种可能的解决方案是使用配置文件中的“debugtoolbar.panels”设置来禁用请求变量面板(这就是问题的根源):
是的,看起来Pyramid对附加的请求对象做了一些有趣的事情。我尝试将db连接附加到资源(根对象)中,并且它可以工作。我更改了cookbook,以便将db对象从gridfs导入gridfs导入pymongo类根中移动到resources.py根对象中(不像cookbook中的请求挂钩)(对象):def uuu init uuuuu(self,request):self.request=request.registry.settings conn=request.registry.conn self.db=conn[settings['db_name']]self.fs=GridFS(self.db)这实际上只是pyramid_debugtoolbar的一个集成问题。如果禁用调试工具栏,问题就会消失。pymongo回答“是的,我有这个属性!”急切是许多类似问题的根源。fwiw您使用的是pyramid 1.2a1而不是1.1此特定问题已在pyramid_调试工具栏0.9中修复。我从pyramid项目的非常基本的版本“pyramid_starter”开始,因此我的视图尽可能简单。定义my_视图…返回{‘project’:‘MyProject’}:-)
db.foo.bar.find(...)
[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