I am writing a file upload page with Django/Python. I get this error: FileNotFoundError: [Errno 2] No such file or directory: '48.png'.
Here is the relevant part of the view, which handles the upload:
`if request.method == 'POST':
form = myform(request.POST, request.FILES)
if form.is_valid():
print("FORM VALID")
f = request.FILES['thefile']
print("f:" + f.name)
print(pref+'/'+str(f))
open(f.name, 'rb')
else:
print('FORM NOT VALID')
Things go wrong in the open(f.name statement. The form is in a template:
<form method="post" action="/dataset_storage_upload/{{ pk }}/{{pid}}/" name="submit" enctype="multipart/form-data">
{% csrf_token %}
{{ theForm.as_p }}
<button type="start_upload" value="Submit" onclick="document.upload.submit() ">Upload</button>
</form>
and this is the form:
class myform(forms.Form):
thefile = forms.FileField()
I have this information in the console
FORM VALID
f:48.png
/17/3/48.png
(/17/3 is the prefix)
In the Traceback, in the browser, under local vars, I have this:
pref '/17/3'
mylist[]
f <InMemoryUploadedFile: 48.png (image/png)>
pk '17'
form <myform bound=True, valid=True, fields=(thefile)>
request<WSGIRequest: POST '/dataset_storage_upload/17/3/'>
I think this tells me that there is a file 48.png in memory. So why is it not there when I open it?
Many thanks for your attention.