I am getting an error setting up this Django web app. I receive a 505 error message whenever I hit the submit button. The image is supposed to be saved in the root directory, but I can't seem to figure out the problem.
Below is the code for the web app
urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from CancerClassifier import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
url('^$', views.index, name = 'homepage'),
url('predictImage', views.predictImage, name='predictImage'),
]
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
views.py:
from django.shortcuts import render
from django.core.files.storage import FileSystemStorage
def index(request):
context = {'a' : 1}
return render(request, 'index.html', context)
def predictImage(request):
print(request)
print(request.POST.dict())
fileObj = request.FILES['filePath']
fs = FileSystemStorage()
fs.save(fileObj.name, fileObj)
context = {'a' : 1}
return render(request, 'index.html', context)
index.html:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Skin Lesion Classifier</title>
</head>
<body>
<h1>Upload new File</h1>
<form class="imageUpload" action="predictImage" method="post">
{% csrf_token %}
<input type="file" name="filePath">
<input type="submit" value="Submit">
<br>
<h3>Prediction: {{prediction}}</h3>
</form>
</body>
</html>
This is the error message i received in the terminal:
[16/Jan/2021 17:13:14] "GET / HTTP/1.1" 200 493
<WSGIRequest: POST '/predictImage'>
{'csrfmiddlewaretoken': 'UPYALDxVQ8SJR8DDy55h14PnaaWElXBwcYnlms6hhpV9qgbtdy1hW6UnU1Y8TVpk', 'filePath': 'image-asset.jpeg'}
Internal Server Error: /predictImage
Traceback (most recent call last):
File "/Users/sparshbohra/opt/anaconda3/lib/python3.7/site-packages/django/utils/datastructures.py", line 76, in __getitem__
list_ = super().__getitem__(key)
KeyError: 'filePath'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/sparshbohra/opt/anaconda3/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Users/sparshbohra/opt/anaconda3/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/sparshbohra/cancer-scanner/CancerProj/CancerClassifier/views.py", line 12, in predictImage
fileObj = request.FILES['filePath']
File "/Users/sparshbohra/opt/anaconda3/lib/python3.7/site-packages/django/utils/datastructures.py", line 78, in __getitem__
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'filePath'
[16/Jan/2021 17:13:19] "POST /predictImage HTTP/1.1" 500 67980