0

我一直在使用 django 1.4 做一个表单集,用户可以用来上传一个或多个图像。

formset 在向导步骤中使用,但在 done 方法中手动处理 is_valid 检查和 formset.save()。

表单集的行为非常奇怪,当我在表单集中选择不同的图像时,它可以完美地工作。但是,当我在两个字段中上传相同的图像时,出现以下错误

ValueError at /item/post-project/

I/O operation on closed file

这很奇怪,不知道如何调试这样的问题,我尝试使用一个函数来替换文件字段中的 upload_to 参数以始终使用 uuid 字段,以防由于文件命名重复而引发此错误,但确实如此不解决问题。

这是我的功能

AttachmentFormSet   = inlineformset_factory(Item, Attachment, extra=attachment_form_extra, max_num=8, can_delete=False, form=AttachmentForm)
formset = AttachmentFormSet(request.POST, request.FILES, instance=instance,)
if formset.is_valid():
  formset.save()

关于如何解决这个问题的任何建议?

错误堆栈

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/item/post-project/

Django Version: 1.4.3
Python Version: 2.7.2
Installed Applications:
('admin_tools',
 'admin_tools.theming',
 'admin_tools.menu',
 'admin_tools.dashboard',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'social_auth',
 'account',
 'item',
 'sponsor',
 'pages',
 'misc.helper')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  20.                 return view_func(request, *args, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/yrok/yrok/apps/item/views.py" in post_item
  105.     return response(request)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/views/generic/base.py" in view
  48.             return self.dispatch(request, *args, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/contrib/formtools/wizard/views.py" in dispatch
  223.         response = super(WizardView, self).dispatch(request, *args, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  69.         return handler(request, *args, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/yrok/yrok/apps/item/wizards.py" in post
  109.                 return self.render_done(form, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/contrib/formtools/wizard/views.py" in render_done
  328.         done_response = self.done(final_form_list, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/yrok/yrok/apps/item/wizards.py" in done
  156.         formset.save()
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/forms/models.py" in save
  497.         return self.save_existing_objects(commit) + self.save_new_objects(commit)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/forms/models.py" in save_new_objects
  628.             self.new_objects.append(self.save_new(form, commit=commit))
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/forms/models.py" in save_new
  731.             obj.save()
File "/Users/mo/Projects/pythonic/yrok-env/yrok/yrok/apps/item/models.py" in save
  327.         super(Attachment, self).save(force_insert=force_insert, force_update=force_update, using=using)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/base.py" in save
  463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/base.py" in save_base
  551.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
  203.         return insert_query(self.model, objs, fields, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
  1593.     return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  911.         for sql, params in self.as_sql():
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in as_sql
  872.                 for obj in self.query.objs
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/fields/files.py" in pre_save
  249.             file.save(file.name, file, save=False)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/fields/files.py" in save
  86.         self.name = self.storage.save(name, content)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/core/files/storage.py" in save
  45.         name = self._save(name, content)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/core/files/storage.py" in _save
  195.                         for chunk in content.chunks():
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/core/files/base.py" in chunks
  63.             self.seek(0)

Exception Type: ValueError at /item/post-project/
Exception Value: I/O operation on closed file
4

0 回答 0