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

2024-05-15 00:50:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我想我可以修改一下this general example如何上传文件以使用photologue的照片模型。我假设这意味着要填充“ImageModel”的ImageField属性“image”。在


Tags: 文件用户模型图像编码界面example画布
1条回答
网友
1楼 · 发布于 2024-05-15 00:50:14

我实际上使用了链接文件上传指南,并将其改编为photologue。我从canvas元素中提取了一个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字符串粘贴到的字段光代码。在

相关问题 更多 >

    热门问题