pythoncherrypygridfsmongoenginerender-to-response

Retrieving an image from mongoengine and display it on a page as http reponse in python


Hello am new with mongoengine. I just want to retrieving an image from mongoengine and display it on a page as http reponse in python. I have added images to database and i printed it in the console and i got the following result:

 <GridFSProxy: 506038485e160077b3efc592>
 genus:m
 family:n
 <GridFSProxy: 5060384a5e160077b3efc595>
 genus:m
 family:n

this is my output when i printed the contents in database.from this i understood that the image got saved into database as objects with ids and now what i want is to display these images into a web page. I use for exampleopen(os.path.join(MEDIA_DIR, u"users/clients.html")) to open a client page. how can i show the image in the client page?


Solution

  • Taking the documentation example you can retrieve a file like so:

    marmot = Animal.objects(genus='Marmota').first()
    photo = marmot.photo.read()
    content_type = marmot.photo.content_type
    

    So you can get the content_type and set the headers correctly and yield the photo as the content