1

I am writing a simple view using Django Model Form, however image field fails to validate with 'This field is required' error message.

I wonder where the problem is...

Model:

class Deal(AbstractModel):
    IMAGE_MAX_LENGTH = 200

    place = models.ForeignKey(Place, related_name='deals', related_query_name='deal', verbose_name=_("Place"))
    image = models.ImageField(default='deals/default.png', max_length=IMAGE_MAX_LENGTH, upload_to='deals', verbose_name=_("Image"))
    ...

View:

@login_required
def deals_create(request):
    # Get place id
    place_id = request.GET.get('place')
    # Get place
    place = Place.objects.get(id=place_id)
    # Process form data
    if request.method == 'POST':
        form = DealsCreateForm(request.POST, request.FILES)
        # Validate post data
        if form.is_valid():
            # Save deal data
            deal = form.save(commit=False)
            deal.place = place
            deal.save()
            # Redirect to reload page and clear post data
            return HttpResponseRedirect(reverse('deal_manager:deals_pending'))
    else:
        form = DealsCreateForm()
    return render(request, 'deal_manager/deals_create.html', {
        'form': form,
        'place': place,
    })

Form:

class DealsCreateForm(ModelForm):

    class Meta:
        model = Deal
        fields = [
            'image', 'product_name', 'product_description',
            'regular_price', 'sale_price', 'limit', 'terms',
        ]

Template:

{% extends "deal_manager/deal_manager.html" %}

{% load i18n %}
{% load staticfiles %}

{% block page_content_body %}
        <div class="row">
            <div class="span12">
                <form action="{% url 'deal_manager:deals_create' %}?place={{ place.id }}" method="post">
                    {% csrf_token %}
                    {{ form.as_p }}
                    <input type="submit" value="{% trans 'Create' %}" />
                </form>
            </div>
        </div>
        <div class="gap gap-small"></div>
{% endblock page_content_body %}

Note: Form validates when I remove the image field from form meta fields.

Note: File uploads works fine with Django Admin for this model.

2
  • Can you include your template code? Commented Apr 1, 2014 at 17:54
  • I've edited the question to include the template code. Commented Apr 1, 2014 at 18:13

1 Answer 1

6

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">
                    ...

Here's the relevant documentation: https://docs.djangoproject.com/en/stable/ref/forms/api/#binding-uploaded-files-to-a-form

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

1 Comment

I knew it was a simple one, but we do need an extra eye when things get messy. Thank you!

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.