开发者

How to Integrate Pyramid 1.1 and Mongo DB - as few lines as possible

开发者 https://www.devze.com 2023-03-30 18:26 出处:网络
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-th

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 "v开发者_如何学Pythonirtualenv\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.


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


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.


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__()
0

精彩评论

暂无评论...
验证码 换一张
取 消