0

I want to upload an image from django forms and process it using OpenCV. Here is the code I've written with the help of 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})

Here process() is the function which processes the uploaded image.Running this code gives me MultiValueDictKey Error on the line where I call process()

After searching for the error, referring to this SO answer I changed process(request.FILES['test_01.png']) to process(request.FILES.get('test_01.png')) I get Attribute Error on the same line(which I guess is because I'm not able to retrieve the uploaded image properly)

Where am I going wrong and what is the correct way of doing so?

mdbhal
  • 99
  • 1
  • 11
  • what is the type of `request.FILES['test_01.ping']`? – ritlew Jan 16 '18 at 17:38
  • ok so while checking the type of `request.FILES['test_01.png']` I get the same `MultiValueDictKey Error` and the type of `request.FILES.get('test_01.png')` is `NoneType` – mdbhal Jan 16 '18 at 17:55
  • have you tried printing out the variable using the pprint library to see what is in the dictionary? it looks like MultiValueDictKey is Django specific – ritlew Jan 16 '18 at 18:00
  • Even after using pprint I get the same error for `request.FILES['test_01.png']` and for `request.FILES.get('test_01.png')` it prints two `None` objects – mdbhal Jan 17 '18 at 15:51

0 Answers0