index.html
<form method="POST" action="." enctype="multipart/form-data">
{% csrf_token %}
{{ image_form.as_p }}
<input type="submit" name="submit" value="Search via Image">
</form>
forms.py
class UploadImageForm(forms.Form):
image_field = forms.ImageField(required=False)
views.py
if request.method == 'POST':
if request.POST.get('submit') == 'Search via Image':
print(request.POST)
print(request.FILES)
因此,我想获取用户上传的图像并将其保存在我的存储器中,对其进行一些操作,然后将其显示在网页上并删除。但是我不能这么做。
在request.POST中,我得到一个包含文件名的字典,但这是纯文本。在使用request.FILES时,
<MultiValueDict: {}>
我应该怎么做?谢谢,谢谢。
答案 0 :(得分:0)
将表单更改为此:
<form method="post" action="." enctype="multipart/form-data">
{% csrf_token %}
{{ image_form.as_p }}
<input type="submit" name="submit" value="Search via Image">
</form>
您在表单中缺少enctype。