为什么我无法两次打开django-uploded文件?

时间:2019-03-28 15:06:22

标签: python django file django-validation django-file-upload

我想在验证器中打开一个上传的文件,以确保该文件正确,然后,如果很好,请在我的视图函数中重新打开该文件以提取数据。

我不确定是否应该那样做。告诉我你的想法。

这是我处于简单状态的验证器:

def validate_file(value):
    with value.open("r") as sf:
        pass  # do some checks with the data
    # now the file is closed 

这是我的视图函数:

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())

我得到这个错误ValueError: I/O operation on closed file.

这是错误堆栈:

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.

我已经尝试过:

def validate_file(value):
   sf = value.open("r")
   # this dosen't close the file 

,这有效,但实际上,我使用此lib来读取和解析数据。 因此,使用lib验证器如下所示:

def validate_file(value):
    import shapefile
    sf = shapefile.Reader(shp=value)

这不起作用,这是错误堆栈:

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.

我不明白为什么我不能第二次打开文件?该库显然关闭了该文件,那么为什么我无法第二次打开它?

你能帮我吗?

0 个答案:

没有答案