所以我有:
资源.py:
def _add(obj, name, parent):
obj.__name__ = name
obj.__parent__ = parent
return obj
class Root(object):
__parent__ = __name__ = None
def __init__(self, request):
super(Root, self).__init__()
self.request = request
self.collection = request.db.post
def __getitem__(self, key):
if u'profile' in key:
return Profile(self.request)
class Profile(dict):
def __init__(self, request):
super(Profile, self).__init__()
self.__name__ = u'profile'
self.__parent__ = Root
self.collection = request.db.posts
def __getitem__(self, name):
post = Dummy(self.collection.find_one(dict(username=name)))
return _add(post, name, self)
我正在使用 MongoDB 和pyramid_mongodb
视图.py:
@view_config(context = Profile, renderer = 'templates/mytemplate.pt')
def test_view(request):
return {}
在 mytemplate.pt 中:
<p tal:repeat='item request.context'>
${item}
</p>
我可以回显数据库中的内容(我正在使用 mongodb),但是当我为每个项目提供 URL 时,使用resource_url()
<p tal:repeat='item request.context'>
<a href='${request.resource_url(item)}'>${item}</a>
</p>
我收到一个错误:'dict' object has no attribute '__name__'
有人可以帮助我吗?