Santana
Santana

Reputation: 175

pyramid traversal resource url no attribute __name__

So I have:

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

and I'm using MongoDB and pyramid_mongodb

views.py:

@view_config(context = Profile, renderer = 'templates/mytemplate.pt')
def test_view(request):
    return {}

and in mytemplate.pt:

 <p tal:repeat='item request.context'>
      ${item}
 </p>

I can echo what's in the database (I'm using mongodb), but when I provided a URL for each item using resource_url()

 <p tal:repeat='item request.context'>
 <a href='${request.resource_url(item)}'>${item}</a>
 </p>

I got an error: 'dict' object has no attribute '__name__', can someone help me?

Upvotes: 0

Views: 679

Answers (1)

Michael Merickel
Michael Merickel

Reputation: 23331

Well a full traceback would sure be useful. However in your example I can at least say that self.__parent__ = Root needs to be using the actual object, and not the class.

Upvotes: 2

Related Questions