Check for if file in form & validity

This commit is contained in:
Aroy-Art 2024-02-22 12:31:18 +01:00
parent e4079e34f2
commit 600d85d7d6
Signed by: Aroy
GPG key ID: 583642324A1D2070

View file

@ -70,6 +70,7 @@ def fileUpload(request):
if request.method == 'POST': if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES) form = UploadFileForm(request.POST, request.FILES)
if form.is_valid(): if form.is_valid():
if 'file' in request.FILES: # Check if a file has been uploaded
file = form.cleaned_data['file'] file = form.cleaned_data['file']
file_name = file.name file_name = file.name
@ -88,9 +89,17 @@ def fileUpload(request):
return HttpResponseRedirect(f"/files/submission/{file_hash}") return HttpResponseRedirect(f"/files/submission/{file_hash}")
else:
# No file was uploaded, add an error message to the context
error_message = 'No file was uploaded.'
return render(request, 'files/upload.html', {'form': form, 'error_message': error_message})
else:
# Form is not valid, add an error message to the context
error_message = 'There was an error with the form.'
return render(request, 'files/upload.html', {'form': form, 'error_message': error_message})
else: else:
form = UploadFileForm() form = UploadFileForm()
return render(request, 'files/upload.html', {'form': form}) return render(request, 'files/upload.html', {'form': form})