python 在 django admin 中验证图像大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1875316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate image size in django admin
提问by Apreche
I see a lot of people with Django apps that have image uploads are automatically resizing the images after they are uploaded. That is well and good for some cases, but I don't want to do this. Instead, I simply want to force the user to upload a file that is already the proper size.
我看到很多使用具有图像上传功能的 Django 应用程序在上传图像后会自动调整图像大小。这在某些情况下很好,但我不想这样做。相反,我只是想强制用户上传一个大小合适的文件。
I want to have an ImageField where I force the user to upload an image that is 100x200. If the image they upload is not exactly that size, I want the admin form to return as invalid. I would also like to be able to do the same thing for aspect ratios. I want to force the user to upload an image that is 16:9 and reject any upload that does not conform.
我想要一个 ImageField,强制用户上传 100x200 的图像。如果他们上传的图片不是那么大,我希望管理表单返回无效。我也希望能够对纵横比做同样的事情。我想强制用户上传 16:9 的图像并拒绝任何不符合的上传。
I already know how to get the width and height of the image, but I can't do that server-side until after the image is already uploaded, and the form is submitted successfully. How can I check this earlier, if possible?
我已经知道如何获取图像的宽度和高度,但是在图像已经上传并且表单提交成功之前我无法在服务器端执行该操作。如果可能的话,我怎样才能更早地检查这个?
回答by Agos
The right place to do this is during form validation.
A quick example (will edit/integrate with more info later):
执行此操作的正确位置是在表单验证期间。
一个快速示例(稍后将编辑/集成更多信息):
from django.core.files.images import get_image_dimensions
from django.contrib import admin
from django import forms
class myForm(forms.ModelForm):
class Meta:
model = myModel
def clean_picture(self):
picture = self.cleaned_data.get("picture")
if not picture:
raise forms.ValidationError("No image!")
else:
w, h = get_image_dimensions(picture)
if w != 100:
raise forms.ValidationError("The image is %i pixel wide. It's supposed to be 100px" % w)
if h != 200:
raise forms.ValidationError("The image is %i pixel high. It's supposed to be 200px" % h)
return picture
class MyAdmin(admin.ModelAdmin):
form = myForm
admin.site.register(Example, MyAdmin)
回答by Muhammad Taqi
use this function in your model file,
在您的模型文件中使用此功能,
from django.core.exceptions import ValidationError
def validate_image(fieldfile_obj):
filesize = fieldfile_obj.file.size
megabyte_limit = 2.0
if filesize > megabyte_limit*1024*1024:
raise ValidationError("Max file size is %sMB" % str(megabyte_limit))
class Company(models.Model):
logo = models.ImageField("Logo", upload_to=upload_logo_to,validators=[validate_image], blank=True, null=True,help_text='Maximum file size allowed is 2Mb')
回答by cethegeek
This may be a dupe of/very similar to this question:
这可能是这个问题的重复/非常相似:
Using jQuery, Restricting File Size Before Uploading
I don't think there is a way to do it the way you want. Agos solution is the way to go...
我不认为有办法按照你想要的方式做到这一点。Agos 解决方案是要走的路...
Edit:One of the answers in the linked SO question talks about using flash to do it. So while it may be possible to do with some other technology, I don't think you can do it with straight javascript.
编辑:链接的 SO 问题中的答案之一谈到使用闪存来做到这一点。因此,虽然可以使用其他一些技术来实现,但我认为您无法使用直接的 javascript 来实现。