0

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)

So I want to take the image uploaded by the user and save it in my storage, perform a few operations on it, display it on the webpage and delete it. But I am not being able to go this.
In request.POST I'm getting a dict containing the file name, but that's plain text. And while using request.FILES I'm getting,

<MultiValueDict: {}>

How should I go about this? Thanks, in advance.

imharjyotbagga
  • 199
  • 4
  • 17

1 Answers1

0

Change your form to this:

<form method="post" action="." enctype="multipart/form-data">
    {% csrf_token %}
   {{ image_form.as_p }}
   <input type="submit" name="submit" value="Search via Image">
</form>

You are missing enctype in your form.

Nalin Dobhal
  • 2,292
  • 2
  • 10
  • 20
  • you are using form in your view, can you show code related to form from your view? – Nalin Dobhal Apr 22 '20 at 13:42
  • I've added the forms.py code. Before saving or doing any other operations I wanted to see the requests and files I'm receiving. So that I can actually know that I've got the file to do further operations like saving the image etc. – imharjyotbagga Apr 22 '20 at 13:49