复制Django表单request.FILES以使用smartfields上传多个图像文件 [英] Replicating Django forms request.FILES to upload multiple image files using smartfields

查看:86
本文介绍了复制Django表单request.FILES以使用smartfields上传多个图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Django表单拍摄单个图像,并使用3个标头下的调整大小后的版本上传它.我什至可以使用request.POST QueryDict来执行此操作,但是即使使用request.FILES MultiValueDict也可以执行此操作,即使它显示了各个字段名称的填充数据也是如此.

I'm trying to take a single image using Django forms and upload it with resized version under 3 headers. I'm even able to do so with request.POST QueryDict but not with request.FILES MultiValueDict even after it shows filled data for respective field names.

我的Views.py

My Views.py

def image_add(request,article_id):
    template_name = 'blogs/image_add.html'
    articles = Article.objects.get(article_id=article_id)
    form = ImageAddForm
    if request.method == 'POST':
        image = request.FILES["image_1080"]
        request.FILES['image_800'] = image
        request.FILES['image_350'] = image
        print(request.POST)
        print(request.FILES)
        form = ImageAddForm(request.POST, request.FILES)
        if form.is_valid():
            new_form = form.save(commit=False)
            new_form.dir_id = article_id
            new_form.save()
            return redirect('/')
    context = {'form':form,'articles':articles}
    return render(request, template_name,context)

我的Models.py

My Models.py

from smartfields import fields
from smartfields.dependencies import FileDependency
from smartfields.processors import ImageProcessor

    class Images(models.Model):
        dir_id = models.CharField(max_length=10,null=True)
        image_1080 = fields.ImageField(upload_to=img_1080_dir_path, name="image_1080", dependencies=[
            FileDependency(processor=ImageProcessor(
                format='PNG', scale={'max_width': 1080, 'max_height': 1080}))
        ])
        image_800 = fields.ImageField(upload_to=img_800_dir_path, blank=True, name="image_800", dependencies=[
            FileDependency(processor=ImageProcessor(
                format='PNG', scale={'max_width': 800, 'max_height': 800}))
        ])
        image_350 = fields.ImageField(upload_to=img_350_dir_path, blank=True, name="image_350", dependencies=[
            FileDependency(processor=ImageProcessor(
                format='PNG', scale={'max_width': 350, 'max_height': 350}))
        ])

我的Forms.py

My Forms.py

class ImageAddForm(forms.ModelForm):
    class Meta:
        model = Images
        fields = ('name','alt_text','image_1080')
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control'}),
            'alt_text': forms.TextInput(attrs={'class': 'form-control'}),
            'image_1080': forms.ClearableFileInput(attrs={'class': 'form-file-input'})
            }

这仅保存一个图像-"image_1080",而不保存其他两个图像.

This saves only one image - 'image_1080' but not the other two.

推荐答案

您覆盖POST参数的方法更像是一种技巧,因为从概念上讲,这意味着您忽略了可能来自用户的数据.因此,我通常会建议您这样做:

Your approach of overwriting POST parameters is more of a hack, because conceptually it means you are ignoring the data that suppose to come in from the user. So this is something I would recommend against in general:

    if request.method == 'POST':
        image = request.FILES["image_1080"]
        request.FILES['image_800'] = image
        request.FILES['image_350'] = image

我怀疑这种方法行不通,因为在许多字段之间共享文件的同一实例.相反,正确的方法是只使用一个表单字段作为输入,然后让智能字段为您将其他字段附加到模型.另外,我建议保持原始文件上传,以防万一将来您需要调整大小并再次更改格式:

I suspect this approach doesn't work because the same instance of the file is shared among many fields. Correct approach instead would be to simply use one form field as input and let smartfields attach other fields to the model for you. Also, I recommend keeping the original file uploaded, just in case if you ever need to resize and change the format again in the future:

from smartfields import utils

class Images(models.Model):
    image = fields.ImageField(
        upload_to=utils.UploadTo(generator=True, field_name='image'),
        dependencies=[
            FileDependency(suffix='1080', processor=ImageProcessor(
                format='PNG', scale={'max_width': 1080, 'max_height': 1080}),
            FileDependency(suffix='800', processor=ImageProcessor(
                format='PNG', scale={'max_width': 800, 'max_height': 800}),
            FileDependency(suffix='350', processor=ImageProcessor(
                format='PNG', scale={'max_width': 350, 'max_height': 350}))
        ])

将图像上传到 image 字段时,smartfields会自动向您的 Images 模型添加额外的字段,例如:

When uploading an image to image field, smartfields will automatically add extra fields to you Images model, eg:

> i = Images(my_image)
> i.save()
> print(i.image)      # original
> print(i.image_1080) # resized to 1080
> print(i.image_800)

这篇关于复制Django表单request.FILES以使用smartfields上传多个图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆