Manpreet Ahluwalia
Manpreet Ahluwalia

Reputation: 331

Django Form Not Validating

I want to implement forgot password functionality. In my verification code function, form is not validating. Here is my Views File:

def getVerCode(request):
    if (request.method == 'POST'):
        form = VerCodeForm(request.POST)
        print(form.errors)
        if (form.is_valid()):
            print('VALID')
            code = request.POST.get('verCode', '')
            print('GETVERCODE : ', verCode)
            if (code == verCode):
                pass
            return HttpResponseRedirect('/custom/GetPassword', {'form': form})
    else:
        form = VerCodeForm()
    return render_to_response('EmailVerification.html', {'form': form})

Template:

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'FormStyle.css' %}" />

{% block body %}
    <div class = "formArea"> {% csrf_token %}
        <label> Enter the verification code sent to your email account: </label>
        {{ hidden_email }}
        <div class="formFields">
            <form method="POST">
                <input type="text" name = "email" size="50"> </input>
                {{ form.verCode.errors }}
                <input type="submit" value="Submit">
            </form>
        </div>
    </div>
{% endblock %}

Form:

class VerCodeForm(forms.Form):
    verCode = forms.CharField(max_length = 10)

I tried to print the form.errors:

It says This Field is Required!

Upvotes: 0

Views: 136

Answers (2)

Ashish Srivastava
Ashish Srivastava

Reputation: 124

A better way would have been, if you had used loop to produce the fields in the template.

Check this out:

{% for field in form %}
<div> 
<label>{{field.label_tag}}</label>
<div> {{field}} </div>
</div>
{% endfor %}

This will help you in preventing these errors in future and writing lot less code in template.

Upvotes: 0

Wiredo
Wiredo

Reputation: 220

You did not name your input on your template correctly. It should be:

<input type="email" size="50" name="verCode"> </input>

Upvotes: 1

Related Questions