0

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

我想让用户从 HTML5 画布上传 base64 编码的图像,但第一步可以从用户的文件系统上传文件。

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

4

1 回答 1

0

我实际上已经使用了链接文件上传指南并将其改编为 photologue。从我的画布元素中,我提取了一个 base64 数据 url 并将表单的字段设置为它的值,然后我可以在 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 是一个非常简单的表单,其中包含粘贴 base64 字符串的字段 photocode。

于 2013-11-30T22:16:57.967 回答