Reputation: 79
I have a Form submission view and a dataupload view. I want to save the form and be redirected to the upload page to upload the file.
I tried return the form submission view in the upload view. The form is getting saved but i am getting MultiValueDictKey error
def uploaddata(request):
return Metadata_submission(request)
if request.method == 'POST':
form = uploadmetaform(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('file_list')
else:
form = uploadmetaform()
return render(request, 'uploaddata.html', {
'form': form
})
This is my form submission view:
def Metadata_submission(request):
print("Metadata Submitted")
Authors_Name = request.POST["Authors_Name"]
Affliations = request.POST["Affliations"]
Dataset_Creation_Date = request.POST["Dataset_Creation_Date"]
Metadata_var = Metadataform(Authors_Name=Authors_Name,
Affliations=Affliations,Dataset_Creation_Date=Dataset_Creation_Dat)
Metadata_var.save()
MultiValueDictKeyError at /uploaddata/ 'Authors_Name'
Upvotes: 0
Views: 62
Reputation: 54
You have to use MultiValueDict's get method. It gives a default value if the contents don't exist. The default value would be false in this case.
Authors_Name = request.POST.get("Authors_Name", False)
Edit: I see someone posted before me the same thing and you said it didn´t work. I would suggest then adding:
from django.core.files.storage import FileSystemStorage
file = request.FILES['file_name']
filesystem = FileSystemStorage()
filename = filesystem.save(file.name, file)
form.save()
In local_settings.py
:
#The url that you want to use in your browser to prefix all uploaded file paths.
MEDIA_URL = '/path/wherever'
#Here you indicate the absolute path
MEDIA_ROOT = os.getcwd() + "/same/path/"
This media folder should have a chmod 755
for DJANGO to be able to store files there.
Edit 2: Changed MEDIA_URL explanation based on dirkgroten's answer.
Upvotes: 1