pptt
pptt

Reputation: 705

Django field validation error is not getting raised

i am trying to allow user to change the password. If form is filled correctly it is changed. But if it is not, then there is no warning or anything, i just get redirected to the same page. I dont understand why, i use the same methods for other forms too.

My form:

class PasswdForm(forms.Form):
    New_password = forms.CharField(widget=forms.PasswordInput(), label='New password', max_length=30)
    Repeat_new_password = forms.CharField(widget=forms.PasswordInput(), label='Repeat new password', max_length=30)

class Meta:
    fields = ('New_password', 'Repeat_new_password')

def clean_new_password(self):
    p1 = self.cleaned_data.get('New_password')
    print ("p1", p1)
    if len(p1) <= 6:
        print ("Password is too short. Minimum 6 symbols.")
        raise forms.ValidationError(
                ('Password is too short. Minimum 6 symbols.'))            

    return p1

I can actually see these prints in my terminal.

My url:

url(r'^loggedin/profile/passwd/$', 'userprofile.views.user_profile_passwd'),

My function:

@login_required
def user_profile_passwd(request):

    args = {'full_name': request.user.username}
    args.update(csrf(request))    

    if request.method == 'POST':
        form = PasswdForm(request.POST)
        if form.is_valid():
            if form.cleaned_data['New_password'] == form.cleaned_data['Repeat_new_password']:

                user = request.user
                passw = make_password(form.cleaned_data['New_password'])
                user.password = passw                

                user.save()

            return render(request,'profile.html')

        else:
            messages.warning(request, "Your password was not changed.")
            args['form'] = PasswdForm()
            return render(request, 'passwd.html', args)
    else:
        form = PasswdForm()    

    args = {'full_name': request.user.username}
    args.update(csrf(request))

    args['form'] = PasswdForm()

    return render(request, 'passwd.html', args)

Does anyone know what could possible be wrong?

Also this is the bloack from my html template:

{% block content %}

<section>
<h2>Change password:</h2>

<form action="/accounts/loggedin/profile/passwd/" method="post"autocomplete="off">{% csrf_token %}

<ul>
{{form.as_ul}}
</ul>


<input type="submit" name="submit" value="Save changes">
</form>

</section>

{% endblock %}

Upvotes: 1

Views: 496

Answers (1)

Todor
Todor

Reputation: 16010

When your form is invalid, you are assigning to args a new empty form. That's why you see no errors.

if request.method == 'POST':
    form = PasswdForm(request.POST)
    if form.is_valid():
        #...
    else:
        messages.warning(request, "Your password was not changed.")
        args['form'] = form #instead of PasswdForm()
        return render(request, 'passwd.html', args)

And please take a read on PEP 8, specially Method Names and Instance Variables your form fields shouldn't start with capital letter.

Upvotes: 3

Related Questions