我想将用户上传的图像存储在 views.py 中的一个变量中(并对其应用一些 opencv 操作),然后显示生成的图像。 我不想将图像存储在数据库中。所以,我不认为模型对于实现这个是必要的。如何进行下一步?以下是 index.html 和 views.py。抱歉,如果有任何编码错误,我是 Django 的新手。
index.html
<form method="POST" enctype="multipart/form-data">{% csrf_token %}
<div class="file-field input-field left">
<div class="btn black-text waves-effect waves-dark" style="background-color: #F5DF73">
<div>Upload</div>
<input type="file" name = "input_image" onchange="readURL(this);">
</div>
<div class="file-path-wrapper">
{#<input class="file-path validate" type="text">#}
</div>
</div>
{{ image }}
</form>
views.py
def upload_pic(request):
if request.method == 'POST':
form = ImageUploadForm(request.POST, request.FILES)
image = form.cleaned_data['image']
return render(request, 'html/index.html', {"image": image})
urls.py
urlpatterns = [
url(r'^$', views.index),
]
请您参考如下方法:
试试这个
form = ImageUploadForm(request.POST)
image = form.cleaned_data['image']
b64_img = base64.b64encode(image.file.read())
return render(request, 'html/index.html', {"image": b64_img})
在 html 中
<img src="data:image/png;base64,{{image}}">
