I am having some problems getting images from the database and showing them in my webpage.
First of all, I am using mongoengine
, so here goes (part of) my models.py
:
class Picture(EmbeddedDocument):
image = ImageField()
class Post(Document):
...
comments = ListField(EmbeddedDocumentField('Comment'))
user = ReferenceField('User', reverse_delete_rule='CASCADE')
images = ListField(EmbeddedDocumentField('Picture'))
Here is the code in my view
where I save the images:
picture = Picture()
with tempfile.TemporaryFile() as f:
f.write(file.read())
f.flush()
f.seek(0)
picture.image.put(f)
picture.save()
post.images.append(picture)
post.save()
So, in the HTML page I have the following:
<p>{{ post.images[0].image }}</p>
<img src="{{ post.images[0].image }}">
The first line outputs: <ImageGridFsProxy: None>
. The second, obviously, doesn't output a valid image.
Here is what I get when I do db.post.find(...)
in the mongo shell:
{ "_id" : ObjectId("56325c4e66b3f6bf8beb9e03"), ... "images" : [ { "image" : ObjectId("56325c4e66b3f6bf8beb9e04") } ] }
Can someone help me with this?
Thanks!