Alex Rivera | Logout

Django: How to check if the user left all fields blank (or to initial values)?

Asked 2011-01-01T00:58:48.517
20

I know is_valid() on a bounded form checks if all required data is entered. This is not what I want. I want to check if any field was filled on the form.

Any ideas?

Elaborating:

I want to give the user the choice of not filling in the form at all. However, if they attempt to fill it in (ie: changed a value in a field from it's initial value---usually empty value), I want to validate it.

The view would be something like this:

def get_opinion(request):

    if request.method == 'POST':
        f = OpinionForm(request.POST)
        if form_is_blank(f):
            return HttpResponseRedirect(reverse('thank_you_anyway'))
        elif f.is_valid():
            #Process here
            return HttpResponseRedirect(reverse('thanks_for_participating'))
    else:
        initial = {'weekday': date.today().strftime('%A')}
        f = OpinionForm(initial=initial)

    return render_to_response(
        'get_opinion.html',
        {'form': f,},
        RequestContext(request)
    )

What I want is the form_is_blank() part.

Edit
Report

1 Answer

1

If you have put required=True in your forms field or in the model blank=False so is_valid() should return False.

answered 2011-01-01T01:03:23.143

Your Answer