0

I've set up the Photologue, but I don't know how to let a (normal) user upload an image without using the admin interface.

I'd like to let users upload base64-encoded images from a HTML5 canvas, but in the first step it would be fine to upload files from the user's filesystem.

I guess I could modify this general example on how to upload files to use photologue's photo model. I assume this would mean somehow filling "ImageModel"'s ImageField attribute "image'.

Community
  • 1
  • 1
kossmoboleat
  • 1,841
  • 1
  • 19
  • 36

1 Answers1

0

I've actually used the linked file upload guide and adapted it to photologue. From my canvas element I extracted a base64 data url and set a form's field to its value then I could interpret it on Django's server side in a view:

def upload_base64(request):
    # Handle file upload
    if request.method == 'POST':
        form = PhotoCodeForm(request.POST, request.FILES)

        if form.is_valid():
            uploaded_photo_data = base64.b64decode(request.POST['photocode'])

            uploaded_photo_file = ContentFile(uploaded_photo_data)            

            title_str = "Untitled"            
            slug = slugify( title_str + str( datetime.now() ) )

            uploaded_photo = Photo.objects.create( image = default_storage.save(slug, uploaded_photo_file),
                                            title = slug,
                                            title_slug = slug )

            name_upload_gallery = "user-upload-queue"                       

            try:
                upload_gallery = Gallery.objects.get(title_slug__exact=name_upload_gallery)      
            except ObjectDoesNotExist:
                return HttpResponseBadRequest('<html><body><p>The gallery "'+name_upload_gallery+'" does not exist.</p></body></html>')

            upload_gallery.photos.add(uploaded_photo)

            # Redirect to the photo gallery after POST
            return HttpResponseRedirect('/canvas/')
        else:
            return HttpResponseBadRequest('<html><body><p>The entered data was not correct.</p></body></html>')
    else:
        form = PhotoCodeForm() # A empty, unbound form

    return render_to_response(
        'photologue_upload/upload_base64.html',
        {'form': form},
        context_instance=RequestContext(request)
    )

upload_base64.html is a very simple form that has a field photocode where the base64 string is pasted to.

kossmoboleat
  • 1,841
  • 1
  • 19
  • 36