django-resized //上传png文件时,其扩展名更改为.apng

时间:2019-06-11 04:13:05

标签: django

我正在使用django调整大小的包来调整图像的大小。

当我使用此包上传PNG文件时,扩展名更改为apng。 我不想更改此扩展名。

其他图像文件通常会上传。

我该怎么解决?

背景 -django == 2.1.5 -django-resized == 0.3.9

使用Django的默认模型ImageField时,png的扩展名不变。

# models.py

def upload_to(instance, filename):
    return 'vip/{username}/{filename}'.format(
        username=instance.whose.whose.username, filename=filename)

class VipIndex(models.Model):
    whose = models.OneToOneField(Profile, on_delete=models.CASCADE, related_name='vipindex')
    main_big_image = ResizedImageField(crop=['middle', 'center'], size=[500, 300], quality=50, blank=True, upload_to=upload_to)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['-created_at']

# forms.py

class VipMainForm(forms.ModelForm):
    class Meta:
        model = VipIndex
        fields = (
            'main_big_image',
        )

    def __init__(self, *args, **kwargs):
        super(VipMainForm, self).__init__(*args, **kwargs)
        self.fields['main_big_image'].widget.attrs = {'autocomplete': 'off', 'class': 'form-control'}


# views.py

@login_required
def profile_2(request):
    if request.method == 'POST':
        form_main = VipMainForm(request.POST, request.FILES)
        if form_main.is_valid():
            nav = form_main.save(commit=False)
            nav.whose = request.user.profiles
            nav.save()
            return redirect('profile_2')

    else:
        form_main = VipMainForm()

    return render(request, 'accounts/profile_2.html', {
        'form_main':form_main,
    })

是否有用于保留png文件扩展名的特定设置?

1 个答案:

答案 0 :(得分:1)

将以下内容添加到settings.py

DJANGORESIZED_DEFAULT_FORMAT_EXTENSIONS = {'PNG': ".png"}

,它将覆盖将文件另存为.apng