Django - 使用表单上传图片有困难

时间:2018-01-16 17:33:37

标签: python django django-forms

我想从django表单上传图像并使用OpenCV处理它。以下是我在Django documentation的帮助下编写的代码:

1.forms.py:

class UploadFileForm(forms.Form):
    image = forms.ImageField()

2.views.py:

def upload_file(request):
    context = {'status':'not working'}
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            context['status'] = 'working'
            process(request.FILES['test_01.png'])
            return render(request,'create.html',context)
    else:
        form = UploadFileForm()
    return render(request, 'create.html', {'form': form})

此处process()是处理上传图片的功能。运行此代码会在我调用MultiValueDictKey Error

的行上显示process()

在搜索错误后,参考this SO answer我将process(request.FILES['test_01.png'])更改为process(request.FILES.get('test_01.png'))我在同一行上获得Attribute Error(我猜是因为我不是能够正确检索上传的图像)

我哪里出错了,这样做的正确方法是什么?

0 个答案:

没有答案