首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让用户上传图片到photologue?

如何让用户上传图片到photologue?
EN

Stack Overflow用户
提问于 2013-04-29 04:13:46
回答 1查看 440关注 0票数 0

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

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

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

EN

回答 1

Stack Overflow用户

发布于 2013-12-01 06:16:58

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

代码语言:javascript
复制
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字符串。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16267338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档