Django sorl:没有足够的值来解压(预期 2,得到 1)
Django sorl: not enough values to unpack (expected 2, got 1)
我正在尝试使用一个表单来根据将要上传的图像生成缩略图
我将使用 sorl 生成缩略图,并遵循以下文档:
- Django 多文件上传:https://docs.djangoproject.com/en/3.0/topics/http/file-uploads/
- Sorl 低水平 API: https://sorl-thumbnail.readthedocs.io/en/latest/examples.html
当我尝试生成缩略图时,我得到了
的错误
not enough values to unpack (expected 2, got 1)
我不明白我做错了什么,总而言之,我上传了图片并将其保存在我的根目录中,然后我尝试创建缩略图
请问有没有什么办法可以取消原图在根目录下的保存呢?我打算将图像和拇指发送到 google 云存储
我的forms.py:
from django import forms
class FileFieldForm(forms.Form):
file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
我的 html 文件:upload.html
<html>
<head></head>
<body>
<h3>Read File Content</h3>
<form enctype="multipart/form-data" action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Save">
</form>
</body>
</html>
我的 views.py 看起来像:
from sorl.thumbnail import ImageField, get_thumbnail
from .forms import FileFieldForm
class FileFieldView(FormView):
form_class = FileFieldForm
template_name = 'app_workflow/upload.html' # Replace with your template.
success_url = '/photo' # Replace with your URL or reverse().
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getlist('file_field')
if form.is_valid():
for f in files:
with open(f.name, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
im = get_thumbnail(f.name, '100x100', crop='center', quality=99)
return self.form_valid(form)
else:
return self.form_invalid(form)
正如您在问题中所说,您不想存储在根目录中并生成缩略图。那么我会建议这样的事情:
from PIL import Image
class FileFieldView(FormView):
form_class = FileFieldForm
template_name = 'app_workflow/upload.html' # Replace with your template.
success_url = '/photo' # Replace with your URL or reverse().
def form_valid(self, *args, **kwargs):
img_size = (100, 100)
files = self.request.FILES.getlist('file_field')
for f in files:
im = Image.open(f)
im.thumbnail(img_size)
# your thumbnail image is in memory now
# you can now store it in your model and use django-storages to upload it to gcloud
return super().form_valid(*args, **kwargs)
这里我不存储图片,直接在PIL.Image
模块中加载生成缩略图。您可以使用 django-storages
将数据从 FileField 上传到 gcloud。
存储在根目录中:
然后你可以这样修改代码:
for f in files:
for chunk in f.chunks():
destination.write(chunk)
im = Image.open(f)
im.thumbnail(img_size)
im.save('thumb_{}'.format(f.name))
我正在尝试使用一个表单来根据将要上传的图像生成缩略图
我将使用 sorl 生成缩略图,并遵循以下文档:
- Django 多文件上传:https://docs.djangoproject.com/en/3.0/topics/http/file-uploads/
- Sorl 低水平 API: https://sorl-thumbnail.readthedocs.io/en/latest/examples.html
当我尝试生成缩略图时,我得到了
的错误not enough values to unpack (expected 2, got 1)
我不明白我做错了什么,总而言之,我上传了图片并将其保存在我的根目录中,然后我尝试创建缩略图
请问有没有什么办法可以取消原图在根目录下的保存呢?我打算将图像和拇指发送到 google 云存储
我的forms.py:
from django import forms
class FileFieldForm(forms.Form):
file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
我的 html 文件:upload.html
<html>
<head></head>
<body>
<h3>Read File Content</h3>
<form enctype="multipart/form-data" action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Save">
</form>
</body>
</html>
我的 views.py 看起来像:
from sorl.thumbnail import ImageField, get_thumbnail
from .forms import FileFieldForm
class FileFieldView(FormView):
form_class = FileFieldForm
template_name = 'app_workflow/upload.html' # Replace with your template.
success_url = '/photo' # Replace with your URL or reverse().
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getlist('file_field')
if form.is_valid():
for f in files:
with open(f.name, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
im = get_thumbnail(f.name, '100x100', crop='center', quality=99)
return self.form_valid(form)
else:
return self.form_invalid(form)
正如您在问题中所说,您不想存储在根目录中并生成缩略图。那么我会建议这样的事情:
from PIL import Image
class FileFieldView(FormView):
form_class = FileFieldForm
template_name = 'app_workflow/upload.html' # Replace with your template.
success_url = '/photo' # Replace with your URL or reverse().
def form_valid(self, *args, **kwargs):
img_size = (100, 100)
files = self.request.FILES.getlist('file_field')
for f in files:
im = Image.open(f)
im.thumbnail(img_size)
# your thumbnail image is in memory now
# you can now store it in your model and use django-storages to upload it to gcloud
return super().form_valid(*args, **kwargs)
这里我不存储图片,直接在PIL.Image
模块中加载生成缩略图。您可以使用 django-storages
将数据从 FileField 上传到 gcloud。
存储在根目录中:
然后你可以这样修改代码:
for f in files:
for chunk in f.chunks():
destination.write(chunk)
im = Image.open(f)
im.thumbnail(img_size)
im.save('thumb_{}'.format(f.name))