【发布时间】:2018-06-26 18:14:03
【问题描述】:
views.py
if request.is_ajax():
testpic = TestPic.objects.get(pk=1)
form = TestPicForm(request.POST, request.FILES, instance=testpic)
if form.is_valid():
form.save()
forms.py
class TestPicForm(forms.ModelForm):
class Meta:
from .models import TestPic
model = TestPic
fields = ('file', 'file_50',)
def save(self):
test_photo = super(TestPicForm, self).save()
file = user_photo.file
image = Image.open(file)
rotated_image = image.rotate(90, expand=True)
cropped_image = rotated_image.resize((300, 300), Image.ANTIALIAS)
cropped_50_image = rotated_image.resize((50, 50), Image.ANTIALIAS)
cropped_image.save(test_photo.file.path)
file.seek(0)
cropped_50_image.save(test_photo.file_50.path)
return user_photo
models.py
class TestPic(models.Model):
file = models.ImageField(null=True, blank=True, default=None, upload_to="photo/%Y/%m/%d")
file_50 = models.ImageField(null=True, blank=True, default=None, upload_to="photo/%Y/%m/%d")
此代码用于上传图像文件,将其大小调整两个不同的大小并保存。
遗憾的是,它仅在字段 file_50 上有任何文件时才有效。
-
1234563
当
file_50字段上没有文件时,它会抛出错误:The 'file_50' attribute has no file associated with it.
file 字段的文件名已更改。)但是 some_file_50.jpeg 的内容已更改。(我可以通过单击 django-admin 上的图像文件链接查看这些更改)只有文件名,some_file_50.jpeg 是不可更改的。
问题:我该如何解决?感谢阅读。
添加回溯:
Internal Server Error: /accounts/crop/
Traceback (most recent call last):
File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\pythonDev\project\upward\chatkaboo\authapp\views.py", line 833, in crop
form.save()
File "D:\pythonDev\project\upward\chatkaboo\authapp\forms.py", line 182, in save
cropped_50_image.save(user_photo.file_50.path)
File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django\db\models\fields\files.py", line 56, in path
self._require_file()
File "D:\pythonDev\interpreters\forMultichat\lib\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 'file_50' attribute has no file associated with it.
添加html、jquery ajax上传代码:
<form method="post" enctype="multipart/form-data" id="formUpload">
<input type="file" name="file" required id="id_file">
</form>
<script>
$(".js-crop-and-upload").click(function () {
var form_upload = $("#formUpload")[0];
var form_data = new FormData(form_upload);
form_data.append('some', "some_val");
$.ajax({
url:'/accounts/crop/',
type:'post',
dataType:'json',
cache:false,
processData: false,
contentType: false,
data:form_data,
success:function (data) {
console.log(data)
}
});
</script>
【问题讨论】:
-
你能添加完整的回溯吗?通常在这种情况下,这意味着某些东西(例如模板)正在尝试专门引用该字段。
-
@MrName 谢谢,我添加了回溯。请再检查一遍好吗?
-
我添加了html和脚本代码。