【问题标题】:Django sorl: not enough values to unpack (expected 2, got 1)Django sorl:没有足够的值来解包(预期 2,得到 1)
【发布时间】:2020-11-05 19:07:06
【问题描述】:

我正在尝试使用一个表单来从将要上传的图像中生成缩略图

我将使用 sorl 生成拇指,并且我遵循以下文档:

当我尝试生成缩略图时,我得到了错误

not enough values to unpack (expected 2, got 1)

我不明白我做错了什么,总而言之,我上传了图像并将其保存在我的根目录中,然后我尝试创建拇指

还有没有办法避免将原始图像保存在根目录中?我打算将图片和拇指都发送到谷歌云存储

我的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)

【问题讨论】:

    标签: python django django-forms django-file-upload sorl-thumbnail


    【解决方案1】:

    正如您在问题中所说,您不想存储在根目录中并生成缩略图。然后我会建议这样的事情:

    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))
    

    【讨论】:

    • 如果我想将它保存到根目录中,(只是为了检查两个图像)我将如何修改代码?谢谢
    • 我收到错误:'TemporaryUploadedFile' 对象没有属性'copy'
    • 得到一个目的地错误,我猜这是由于缺少:以 open(f.name, 'wb+') 作为目的地:但如果我添加我得到错误 save() missing 1 required positional参数:'fp'
    猜你喜欢
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    相关资源
    最近更新 更多