Devi Sunuwar
Devi Sunuwar

Reputation: 1

Django Form Validation: It is not showing me django error message in template

Here I want to show validationerror on template but it is not returning the error before i even type the password. I want to check the newpassword entered by the user and match the re-entered password, but it's not showing me the error message though it isnot accepting the form if the password arenot matched.

This is my views.py class PasswordsChangeView(FormView): template_name = 'dashboard/password/password_change.html' form_class = ChangePasswordForm success_url = reverse_lazy('dashboard:admin_login')

def get_form(self):
    form = super().get_form()
    form.set_user(self.request.user)
    return form

This is forms

class ChangePasswordForm(forms.Form):
old_password = forms.CharField(
    widget=forms.PasswordInput(
        attrs={'class': 'form-control', 'placeholder':  'Password'}))

new_password1 = forms.CharField(
    widget=forms.PasswordInput(
        attrs={'class': 'form-control', 'placeholder':  'Password'}))

new_password2 = forms.CharField(
    widget=forms.PasswordInput(
        attrs={'class': 'form-control', 'placeholder':  'Password'}))      

def set_user(self, user):
    self.user = user
    
def clean(self):
    cleaned_data = super().clean() 
    old_password = self.cleaned_data.get('old_password')
    valpwd = self.cleaned_data.get('new_password1')
    valrpwd = self.cleaned_data.get('new_password2')
    
    if valpwd != valrpwd:
        raise forms.ValidationError({
            'new_password1':'Password Not Matched'})
    return self.cleaned_data

This is template

<form method="POST">
        {% csrf_token %}
        <div class="input-group mb-3">
          <h6>Old Password</h6>{{form.old_password}}
          <div class="input-group-append">
            <div class="input-group-text">
               <span class="fas fa-lock"></span>
            </div>
          </div>
          </div>
        <div class="input-group mb-3">
          <h6>New Password</h6> {{form.new_password1}}
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-lock"></span>
            </div>
          </div>
        </div>
        <div class="input-group mb-3">
          <h6>Re-Type Password</h6>
          {{form.new_password2}}
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-lock"></span>
            </div>
          </div>
       
            <span class='form-error'> {{form.errors.new_password1}}</span>
  
        </div> 
         {% comment %} {{ form.as_p }} {% endcomment %}
          <button class="btn btn-secondary" type="submit">Change Password</button>

      </form>

Upvotes: 0

Views: 113

Answers (1)

beatmaister
beatmaister

Reputation: 395

I believe you have to include a form.errors tag within the form like this

<form method="POST">
            **{{form.errors}}**
            {% csrf_token %}
            <div class="input-group mb-3">
              <h6>Old Password</h6>{{form.old_password}}
              <div class="input-group-append">
                <div class="input-group-text">
                   <span class="fas fa-lock"></span>
                </div>
              </div>
              </div>
            <div class="input-group mb-3">
              <h6>New Password</h6> {{form.new_password1}}
              <div class="input-group-append">
                <div class="input-group-text">
                  <span class="fas fa-lock"></span>
                </div>
              </div>
            </div>
            <div class="input-group mb-3">
              <h6>Re-Type Password</h6>
              {{form.new_password2}}
              <div class="input-group-append">
                <div class="input-group-text">
                  <span class="fas fa-lock"></span>
                </div>
              </div>
          </div> 
             {% comment %} {{ form.as_p }} {% endcomment %}
              <button class="btn btn-secondary" type="submit">Change Password</button>
    
          </form>

Upvotes: 0

Related Questions