'Django forms with image fields validation

I have simple form with 2 fields: Name, Photo.

class MyForm(ModelForm):
class Meta:
    model = My


class My(models.Model):
    Name = models.models.CharField(max_length=512)
    Photo = models.models.ImageField(upload_to=uploader)

I'am trying to validate and save this model. User chooses photo and doesn't enter name. Form does not pass validation. And user has error message. Then he enters name, but doesn't choose photo. And form doesn't validate again. But user has already chose photo.

I found article, where author explained this. But he wrote, that information out of date. Tnx for help.



Solution 1:[1]

You'll need to include enctype="multipart/form-data" in order to bind imagefield and filefield data to the form. Without that, those fields won't validate.

{% block page_content_body %}
    <div class="row">
        <div class="span12">
            **<form enctype="multipart/form-data"** action="{% url 'deal_manager:deals_create' %}?place={{ place.id }}" method="post">
                ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vaibhav Patel