0

How do say to ImageField that maximum allowed width is 500 and only *.JPGs are allowed? If it is not possible to do it in ImageField, what are the ways to implement it? Or should it be specified on the html form? Thanks in advance

1

1 Answer 1

1

You can check this in form clean methods:

def clean_picture(self):
    # check image weight
    image = self.cleaned_data['picture']
    if image:
        if image.width > 100:
            raise forms.ValidationError(_("Selected image is too wide"))
    return image

There is also height attribute. You can access name of the file in ImageField, get extension from it and validate it like I did with width.

Sign up to request clarification or add additional context in comments.

3 Comments

is it possible to check this before uploading?
You can use js to do this, e. g. jquerybyexample.blogspot.com/2012/03/…
the image does not have attribute "width" ( Anyway, I did it using PIL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.