如何集成 Pyramid 1.1 和 Mongo DB - 尽可能少的行

发布于 2024-12-01 19:11:36 字数 2798 浏览 2 评论 0原文

目标:我尝试将 Mongo DB 与 Pyramid 1.1 基本应用程序集成。

背景:应用程序是由本书(https://docs.pylonsproject.org/projects/pyramid/1.1/narr/project.html#creating-the-project)使用基本命令“粘贴创建”创建的-tpyramid_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.

Goal: I try to integrate Mongo DB with Pyramid 1.1 basic application.

Background: Appliation is created by the book (https://docs.pylonsproject.org/projects/pyramid/1.1/narr/project.html#creating-the-project) using basic command "paste create -t pyramid_starter"

I followed this cookbook article: https://docs.pylonsproject.org/projects/pyramid_cookbook/dev/mongo.html

Problem: It seems that when ever I add MongoDB connection into request I got "Internal Server Error" with

I have tried several articles and it seems that I must start debug system more?
Has anybody found easy solution for this?

Exception if it helps some expert

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨巷深深 2024-12-08 19:11:36

另一种可能的解决方案是使用配置文件中的“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

Another possible solution is to use the 'debugtoolbar.panels' setting in your config file to disable the request_vars panel (which is what is causing the issue):

[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
愛上了 2024-12-08 19:11:36

Pymongo 的 DatabaseCollection 对象响应 __getattr__ 以提供更好的界面并让您编写如下代码:

db.foo.bar.find(...)

任何对 __getattr__ 的调用 会成功,但不幸的是,这会混淆一些期望某些属性可调用的库(Pymongo Collection 对象不可调用,除非引发您在上面看到的异常)。

我在金字塔项目中所做的只是在资源中使用数据库,以防止对数据库或集合的引用出现在视图或其他代码的模块级别中。作为一个额外的好处,这最终成为强制关注点分离的好方法,以便资源处理数据库操作,而视图仅将其转换为在模板中显示。

Pymongo's Database and Collection objects respond to __getattr__ in order to provide a nicer interface and let you write code like:

db.foo.bar.find(...)

Any call to __getattr__ will succeed, but unfortunately this confuses some libraries which expect certain attributes to be callable (the Pymongo Collection object is not callable, except to raise that exception you're seeing above).

What I've done in Pyramid projects is only use the database from within the resources, to prevent references to the database or collections from becoming present in the module level in views or other code. As an added benefit, this ends up being a good way of enforcing separation of concerns so that resources handle database manipulation, and views only translate that for display in the templates.

作业与我同在 2024-12-08 19:11:36

该错误意味着您尝试调用数据库实例中不存在的方法 (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.

如果您没有修改代码,那么可能是 markupsafe 中的一个错误,尝试在数据库实例中调用 html()

s.__html__()

That error means that your trying to call a method (html) that doesn't exist in the Database instance.

>>> 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.

If you haven't modified the code then it is possible it's an bug in markupsafe that tries to call html() in a Database instance

s.__html__()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文