ratbaby
ratbaby

Reputation: 279

NDB key.get() gives 'unicode' object has no attribute 'get' error

I am migrating from db to ndb. There is a function to dynamically serve images in a path. In DB, i pass entity key(i.key()) as img_id and using db.get(key) I get the image.

For NDB, i tried to pass key (i.key) and entity key(i.key.urlsafe()) to pass. But both these cases i get 'unicode' object has no attribute 'get' error.

**DB:**
def get(self):
image = db.get(self.request.get('img_id'))

**HTML**
<a class="post-title" href ="{{permalink}}">{{i.content}}</a>
<img src="/img?img_id={{i.key}}"></img>

**NDB**
class ImageHandler(BlogHandler):
def get(self):
    img=self.request.get('img_id')
    image=img.get()
    if image.image:
        self.response.headers['Content-Type'] = 'image/png'
        self.response.out.write(image.image)
    else:
        self.response.out.write('No image')     

I couldn't able find the mistake in this. Am i passing wrong type for key in key.get()?

Upvotes: 2

Views: 1186

Answers (1)

Thanos Makris
Thanos Makris

Reputation: 3115

You could try the following:

HTML

<a class="post-title" href ="{{ permalink }}">{{ i.content }}</a>
<img src="/img?img_id={{ i.key.urlsafe() }}"></img>

NDB

class ImageHandler(BlogHandler):
def get(self):
    img_key = ndb.Key(urlsafe=self.request.get('img_id'))
    image = img_key.get()
    if image.image:
        self.response.headers['Content-Type'] = 'image/png'
        self.response.out.write(image.image)
    else:
        self.response.out.write('No image')  

Hope this helps.

Upvotes: 8

Related Questions