将表单imagefield保存到djang视图中的另一个图像字段

2024-09-28 19:23:39 发布

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

我有以下视图,在保存之前将一个图像字段保存到另一个图像字段:

if request.method == 'POST':
        form = PlayerForm(request.POST, request.FILES, instance=current_player)
        if form.is_valid():

            temp_image = form.cleaned_data['profile_image2']
            form.cleaned_data['profile_image'] = temp_image
            form.profile_image = temp_image


            form.save() 

            return redirect('player')

问题是图像无法保存。我用boto作为后端。我不确定这是否与此有关。在

如何将临时图像保存到配置文件映像?在


Tags: 图像imageform视图dataifrequestfiles
2条回答

我想您可能需要先将表单保存到模型中,然后再更新profile_image

from django.core.files.base import ContentFile

if form.is_valid():
    new_player = form.save()

    temp_image = new_player.profile_image2
    # duplicate the image for "profile_image2"
    dup_file = ContentFile(temp_image.read())
    dup_file.name = temp_image.name
    new_player.profile_image = dup_file
    new_player.save()

你的代码让我有点困惑。{{1}你把它理解成是从cd1}得到的吗?如果这就是你正在尝试的,那么这是一个可能的答案:

image = form['profile_image2']
photo = PlayerForm(profile_image=image)
photo.save()

[这里是初学者,所以可能会有一个小错误,但如果我在做你正在做的事情,那就是我将如何着手解决这个问题]

相关问题 更多 >