Как загрузить изображение из формы Django без модели
Столкнулся с проблемой что не знаю как вывести из формы изображение. Из документации тоже мало что понятно. forms.py
from django import forms
class ImageForm(forms.Form):
file_upload = forms.FileField()
views.py
from django.shortcuts import render
from .forms import ImageForm
# Create your views here.
from PIL import Image
from pytesseract import pytesseract
def image_upload(request):
context = dict()
if request.method == 'POST':
form = ImageForm(request.POST,request.FILES)
if form.is_valid():
image = Image.open(request.FILES['file_upload'])
image_text = pytesseract.image_to_string(image)
image_picture = request.FILES['file_upload']
context.update({'image_text':image_text,'image_pic':image_picture })
form = ImageForm()
else:
form = ImageForm()
context.update({'form':form,})
print(context)
return render(request,'main/index.html',context)
index.html
<!DOCTYPE html>
<html>
<body>
<title>test</title>
<div>
<form method="post" enctype="multipart/form-data" action="{% url 'main' %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">UpLoad</button>
</form>
</div>
<div>
{% if image_text %}
<p>Succesfully {{ image_text }}</p>
<img src="{{ image_pic }}"/>
{% endif %}
</div>
</body>
</html>
В документации сказано что нужно обрабатывать файл и был приведен такой пример
def handle_uploaded_file(f):
with open('some/file/name.txt', 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
Но откуда я могу узнать где храниться файл?Как здесь сохранить изображение без модели?И как его вывести в шаблоне index.html