问题描述
目标:我尝试将Mongo DB与Pyramid 1.1基本应用程序集成.
Goal: I try to integrate Mongo DB with Pyramid 1.1 basic application.
背景:应用是使用基本命令由书(https://docs.pylonsproject.org/projects/pyramid/1.1/narr/project.html#creating-the-project)创建的粘贴创建-t pyramid_starter"
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"
我遵循了这篇食谱文章: https://docs.pylonsproject.org/projects/pyramid_cookbook/dev/mongo.html
I followed this cookbook article: https://docs.pylonsproject.org/projects/pyramid_cookbook/dev/mongo.html
问题:似乎我每次将MongoDB连接添加到请求中时,都会出现
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.
推荐答案
Pymongo的Database
和Collection
对象响应__getattr__
以便提供更好的界面,并让您编写如下代码:
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(...)
对__getattr__
的任何调用都将成功,但是不幸的是,这使某些希望某些属性可调用的库感到困惑(Pymongo Collection
对象不可调用,除了会引发您在上面看到的异常).
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).
我在Pyramid项目中所做的只是在资源中使用数据库,以防止在视图或其他代码的模块级别出现对数据库或集合的引用.另外一个好处是,这最终成为强制分离关注点的好方法,以便资源可以处理数据库操作,而视图只能将其转换为显示在模板中.
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.
这篇关于如何集成Pyramid 1.1和Mongo DB-尽可能少的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!