我想根据 a 的值来获取 aCharField
的值ImageField
。我的表单和视图定义为:
#Form
class GpsImForm(forms.Form):
image = forms.ImageField(required=True)
gps_data = forms.CharField(required=True)
#View
def gpsim_gen_view(request):
if request.method == 'POST':
form = GpsImForm(request.POST, request.FILES)
if 'image' in request.FILES:
im = request.FILES['image']
i = Image.open(im)
... # functions to extract exif data from i
request.POST.update({ 'gps_data': ...}) # set gps_data based on exif data from i
if form.is_valid():
obj = form.save()
return ... #returns the gpsim
else:
form = GpsImForm()
return direct_to_template(request, 'gpsim_generate.html', {'form': form,})
已gps_data
更新,但是,一旦我使用Image.open()
,我就会收到以下错误消息:
上传有效图片。您上传的文件不是图像或损坏的图像。
如果我评论有关的行i
并将其修改gps_data
为任何内容,则表单(带有图像)将被保存而没有任何错误......
# i = Image.open(im)
# ...
# functions to extract exif data from i
request.POST.update({ 'gps_data': 'some text'}) # set gps_data to 'test'