所以这是上下文。我正在使用Django和Python3.6,我们正在使用Django REST Framework来构建我们的REST API。我在页面上有一个简单的表单,但它不是直接提交的。我正在使用axios手动调用我们的API。
这是按下提交按钮时使用的代码:
let form = new FormData();
form.append('name', this.game.name);
form.append('video', this.game.video);
form.append('icon', this.game.icon);
axios.post("api/games/", form, {headers: {
'Content-Type': 'multipart/form-data',
}})
这里一切都很好。我的API收到正确的数据,我可以轻松创建我的游戏对象。
主要问题是'视频'领域。它在我的models.py
中注册为FileFieldname = models.CharField(max_length=255, unique=True)
icon = models.ImageField(upload_to=user_directory_path)
video = models.FileField(upload_to=user_directory_path)
提交类似500ko的MP4视频文件(.mp4)时,它正常工作。
但是当我尝试上传一个更大的文件,比如5个月,格式为QT(.mov)时,我从django收到错误:
ERROR 2017-11-23 16:59:31,831 exception Internal Server Error: /api/games/
Traceback (most recent call last):
File "myawesomeproject/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "myawesomeproject/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "myawesomeproject/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "myawesomeproject/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/viewsets.py", line 90, in view
return self.dispatch(request, *args, **kwargs)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/views.py", line 489, in dispatch
response = self.handle_exception(exc)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/views.py", line 449, in handle_exception
self.raise_uncaught_exception(exc)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/views.py", line 486, in dispatch
response = handler(request, *args, **kwargs)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/mixins.py", line 21, in create
self.perform_create(serializer)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/mixins.py", line 26, in perform_create
serializer.save()
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/serializers.py", line 214, in save
self.instance = self.create(validated_data)
File "myawesomeproject/api/serializers.py", line 114, in create
return super().create(validated_data)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/serializers.py", line 913, in create
instance = ModelClass.objects.create(**validated_data)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/base.py", line 808, in save
force_update=force_update, update_fields=update_fields)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/base.py", line 838, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/base.py", line 924, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/base.py", line 963, in _do_insert
using=using, raw=raw)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1106, in execute_sql
for sql, params in self.as_sql():
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1059, in as_sql
for obj in self.query.objs
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1059, in <listcomp>
for obj in self.query.objs
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1058, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1008, in pre_save_val
return field.pre_save(obj, add=True)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/fields/files.py", line 296, in pre_save
file.save(file.name, file.file, save=False)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/fields/files.py", line 94, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "myawesomeproject/lib/python3.6/site-packages/django/core/files/storage.py", line 54, in save
return self._save(name, content)
File "myawesomeproject/lib/python3.6/site-packages/django/core/files/storage.py", line 338, in _save
file_move_safe(content.temporary_file_path(), full_path)
File "myawesomeproject/lib/python3.6/site-packages/django/core/files/move.py", line 58, in file_move_safe
with open(old_file_name, 'rb') as old_file:
FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/wc/6cny2lg147l55mt4zm92zw9m0000gn/T/tmp6xuujgz2.upload'
我在网上看到,Django根据文件大小的不同处理文件,这让我相信它来自于此。
但除了一些信息,我无法找到解决问题的方法。
我在这里做错了吗?
答案 0 :(得分:1)
我已解决了我的问题。我犯了一个可怕的错误,抱歉。
我在验证步骤中重命名了我的文件。问题是它适用于<2.5Mb的图像和视频,因此我认为它不会来自它。
为了解决这个问题,我删除了验证器中的重命名,并编辑了我在FileField的upload_to参数中设置的方法。