如何让用户将图像上传到photologue?

时间:2013-04-28 20:13:46

标签: python photologue

我已经设置了Photologue,但我不知道如何让(普通)用户在不使用管理界面的情况下上传图片。

我想让用户从HTML5画布上传base64编码的图像,但在第一步中,从用户的文件系统上传文件会很好。

我想我可以修改this general example如何上传文件以使用photologue的照片模型。我认为这意味着以某种方式填充“ImageModel”的ImageField属性“image”。

1 个答案:

答案 0 :(得分:0)

我实际上已经使用了链接文件上传指南并将其改编为photologue。从我的canvas元素中我提取了一个base64数据url并将一个form的字段设置为它的值然后我可以在一个视图中在Django的服务器端解释它:

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是一个非常简单的表单,它有一个字段photocode,其中粘贴了base64字符串。