Python Forum

Full Version: storing images in mongodb using python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How do I store and retrieve/display images in mongodb.I am working with Pycharm as the editor .
when I upload a file through html and try to store it generally then only the name of the image is getting stored. I want to display the images stored in a new html page.
Richa.
Show the code you use and html page where you want to display the image. We don't even know what modules you use.
My python code is as follows:

from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.template import loader
from django.views.decorators.csrf import csrf_exempt
from pymongo import MongoClient
import gridfs
client = MongoClient('localhost:27017')
# disabling csrf (cross site request forgery)
@csrf_exempt
def index(request):
    # if post request came
    if request.method == 'POST':
        db = client.rich
        st=request.POST.get('pic',0);
        db.Employees.insert(
            {
            "pic": st
            }
        )
        item=db.Employees.find({})
        for x in item:
            html = "<html><body><img src='%s'></body></html>", x['pic']
        return HttpResponse(html)

    else:

        template = loader.get_template('index.html')
        return HttpResponse(template.render())