当我尝试从邮寄表单中清除邮寄封面时,我得到以下错误:
raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
ValueError: The 'postcover' attribute has no file associated with it.
例如用于附件的工作正常。
models.py
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(verbose_name="Post Title", max_length=40)
content = models.TextField(verbose_name="Post Content", max_length=10000)
tag = models.CharField(verbose_name="Tags/Meta - (sep. by comma)", max_length=75, blank=True)
category = models.ForeignKey(Category, verbose_name="Category", on_delete=models.CASCADE, null=True)
postattachment = fields.FileField(
verbose_name="Post Attachment",
blank=True,
null=True,
upload_to=get_file_path_user_uploads,
validators=[file_extension_postattachment, file_size_postattachment]
)
postcover = fields.ImageField(
verbose_name="Post Cover",
blank=True,
null=True,
upload_to=get_file_path_user_uploads,
validators=[default_image_size, default_image_file_extension],
dependencies=[FileDependency(processor=ImageProcessor(
format='PNG', quality=99, scale={'max_width': 700, 'max_height': 700}))])
published_date = models.DateField(auto_now_add=True, null=True)
...
forms.py
class PostForm(forms.ModelForm):
class Meta:
model = Post
widgets = {
'title': forms.TextInput(attrs={'placeholder': 'e.g.: Its a sunny day'}),
'tag': forms.TextInput(attrs={
'placeholder': 'e.g.: Cars, Blue, Red, Green'}),
}
fields = ['title',
'category',
'content',
'tag',
'postcover',
'postattachment'
]
captcha = CaptchaField()
field_order = ['title',
'category',
'content',
'tag',
'postcover',
'postattachment',
'captcha'
]
def __init__(self, *args, **kwargs):
kwargs.setdefault('label_suffix', '')
super(PostForm, self).__init__(*args, **kwargs)
self.fields['title'].widget.attrs.update({'class': 'class-four-input-fields fieldbreak'})
...
self.fields['postcover'].widget.attrs.update({'class': 'fieldbreak'})
self.fields['postcover'].label = mark_safe('Cover:')
self.fields['postcover'].help_text = mark_safe("<h4 class='help_text'>→ Choose a Cover for your Post (max. 2 MB)</h4>")
从我的角度来看,还不清楚为什么会这样。 在字面上没有任何区别,我可以清除而没有任何问题,并且在models.py上,它们都将null和blank设置为True?
views.py
def post_edit(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.user == post.author:
if request.method == "POST":
form = PostForm(request.POST, request.FILES, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.published_date = timezone.now()
post.save()
return redirect('post_detail', pk=post.pk)
else:
form = PostForm(instance=post)
return render(request, 'app/Post/post_edit.html', {'form': form})
else:
messages.success(request, 'Uups, something went wrong, please try again or contact the support.')
return redirect('post_detail', pk=post.pk)
完整跟踪:
完整跟踪:
Traceback (most recent call last):
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/user123/docs/PyCharm/app/app_Accounts/decorators.py", line 33, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/ratelimit/decorators.py", line 30, in _wrapped
return fn(*args, **kw)
File "/home/user123/docs/PyCharm/app/app/views.py", line 473, in post_edit
post.save()
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/smartfields/models.py", line 28, in save
super(SmartfieldsModelMixin, self).save(*args, **kwargs)
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/django/db/models/base.py", line 718, in save
force_update=force_update, update_fields=update_fields)
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/django/db/models/base.py", line 748, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/django/db/models/base.py", line 809, in _save_table
for f in non_pks]
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/django/db/models/base.py", line 809, in <listcomp>
for f in non_pks]
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/smartfields/fields/__init__.py", line 79, in pre_save
self.manager.process(model_instance)
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/smartfields/managers.py", line 154, in process
self._process(d, instance)
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/smartfields/managers.py", line 130, in _process
dependency.process(instance, value, progress_setter=progress_setter)
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/smartfields/dependencies.py", line 206, in process
**self._processor_params
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/smartfields/processors/base.py", line 23, in __call__
stashed_value=stashed_value, **self.get_params(**kwargs))
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/smartfields/processors/image.py", line 287, in process
cur_pos = value.tell()
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/django/core/files/utils.py", line 21, in <lambda>
tell = property(lambda self: self.file.tell)
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/django/db/models/fields/files.py", line 41, in _get_file
self._require_file()
File "/home/user123/docs/PyCharm/app/venv/lib/python3.6/site-packages/django/db/models/fields/files.py", line 38, in _require_file
raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
ValueError: The 'postcover' attribute has no file associated with it.
任何想法,亲切的问候。
答案 0 :(得分:0)
此行为的根本原因是以下行:
...
dependencies=[FileDependency(processor=ImageProcessor(
format='PNG', quality=99, scale={'max_width': 700, 'max_height': 700}))])
我目前对此没有任何解决方法,但似乎imageprocess总是期望在该字段上有什么东西