I would like to open an uploaded file in a validator to make sure the file is good, then if it's good re-open it in my view function to extract the data.
I'm not sure if it supposed to be done like that. Tell me what you think.
Here is my validator at it's simple state:
def validate_file(value):
with value.open("r") as sf:
pass # do some checks with the data
# now the file is closed
and this is my view function :
def home(request):
if request.method == 'POST':
# create 2 form
form_message = Filtre_message_form(request.POST)
form_spatial = Filtre_spatial_form(request.POST, request.FILES)
if all((form_message.is_valid(), form_spatial.is_valid())):
# if the file is good open it
f = request.FILES['file'].open(mode='r')
# do some stuf with it
else:
# create 2 empty form
form_message = Filtre_message_form()
form_spatial = Filtre_spatial_form()
return render(request, 'home.html', locals())
And i get this error ValueError: I/O operation on closed file.
Here is the error stack :
Traceback (most recent call last):
File "H:\workspace\test_Django\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "H:\workspace\test_Django\venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "H:\workspace\test_Django\venv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "H:\workspace\test_Django\django_test\app_test\views.py", line 16, in home
f = request.FILES['file'].open(mode='r')
File "H:\workspace\test_Django\venv\lib\site-packages\django\core\files\uploadedfile.py", line 87, in open
self.file.seek(0)
ValueError: I/O operation on closed file.
I've tried this :
def validate_file(value):
sf = value.open("r")
# this dosen't close the file
and this works, but in reality i use this lib to read and parse the data. So using the lib the validator look like this:
def validate_file(value):
import shapefile
sf = shapefile.Reader(shp=value)
and this dosen't work, here is the error stack :
Internal Server Error: /accueil/
Traceback (most recent call last):
File "H:\workspace\test_Django\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "H:\workspace\test_Django\venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "H:\workspace\test_Django\venv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "H:\workspace\test_Django\django_test\app_test\views.py", line 16, in home
f = request.FILES['file'].open(mode='r')
File "H:\workspace\test_Django\venv\lib\site-packages\django\core\files\uploadedfile.py", line 87, in open
self.file.seek(0)
ValueError: I/O operation on closed file.
I don't understant why i can't open the file a second time ? The library apparently close the file, so why I'm unnable to open it a second time ?
Can you help me please?