我正在尝试Django中的文件上载 - 我在保存带有文件字段的表单方面遇到了问题。我对此有类似的看法,工作得非常好,我不明白为什么会在这个问题上引发错误。
from django.conf import settings
from django.core.files.storage import FileSystemStorage
def view1(request):
...
if form.is_valid():
form.save()
...
然后在我的models.py
中def upload_path(var1, var2, var3):
return "{}/{}/{}/".format(var1, var2, var3)
这是追溯
Internal Server Error: /app/view1/
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Code\app\views.py", line 216, in view1
form.save()
File "C:\Python27\lib\site-packages\django\forms\models.py", line 463, in save
self.instance.save()
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 837, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 923, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 962, in _do_insert
using=using, raw=raw)
File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 1098, in execute_sql
for sql, params in self.as_sql():
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 1051, in as_sql
for obj in self.query.objs
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 1000, in pre_save_val
return field.pre_save(obj, add=True)
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py", line 296, in pre_save
file.save(file.name, file.file, save=False)
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py", line 93, in save
name = self.field.generate_filename(self.instance, name)
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py", line 327, in generate_filename
filename = self.upload_to(instance, filename)
TypeError: upload_path() takes exactly 3 arguments (2 given)
有人可以向我解释这个错误吗? 感谢
修改
models.py
class Model1(models.Model):
certno = models.CharField(max_length = 20)
datecreated = models.DateTimeField(auto_now_add = True)
pdf = models.FileField(upload_to = upload_path("apple", "cherry", "grapefruit"), null = True, blank = True)
def __unicode__(self):
return str(self.certno)
forms.py
class PatForm(forms.ModelForm):
class Meta:
model = Pat
exclude = ['datecreated']
labels = {
'certno': _('Certificate Number'),
'pdf': _('PDF'),
}
答案 0 :(得分:3)
您的upload_path
函数必须接受2个参数instance
和filename
,但您已经定义了一个自定义函数,它接受3个参数,恰好是字符串。
您应该更改upload_path
功能,可能更像这样,
def upload_path(instance, filename):
return "{}/{}".format(instance.certno, filename)
每当保存模型时,都会调用 upload_path
函数或传递到upload_to
中FileField
选项的任何函数,并使用如上所述的明确参数。