Created
September 9, 2012 20:02
-
-
Save sbose78/3686897 to your computer and use it in GitHub Desktop.
Uploading files in Django using pymongo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import gridfs | |
from django.http import HttpResponse | |
from pymongo.connection import Connection | |
from django.shortcuts import get_object_or_404, render_to_response | |
from django.http import HttpResponseRedirect, HttpResponse | |
from django.template import Context, RequestContext,loader | |
from bson.object import ObjectId | |
#uploading form including image/file | |
def process_health_case(request): | |
about = request.POST['about'] | |
details = request.POST['narrative_text'] | |
connection = Connection('mongodb://sbose78:[email protected]:10068/BOSE') | |
db=connection['BOSE'] | |
collection = db['controller'] | |
if 'image_scan' in request.FILES.keys(): | |
image=request.FILES['image_scan'] | |
fs=gridfs.GridFS(db) | |
file_id = fs.put(image,filename="image_scan3") | |
data={ "about":about, "file_id":file_id} | |
collection.insert(data) | |
else: | |
data={"about":about,"details":details} | |
collection.insert(data) | |
return render_to_response('home/new_narrative.html',{ }, context_instance=RequestContext(request)) | |
# rendering the image | |
def my_image(request,image_id): | |
connection = Connection('mongodb://sbose78:[email protected]:10068/BOSE') | |
db=connection['BOSE'] | |
fs=gridfs.GridFS(db) | |
image_data = fs.get(ObjectId(image_id)) | |
return HttpResponse(image_data, mimetype="image/png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment