0

I have problem with error in coding django not displaying validation error I dont know why django is not displaying the validationerror . Can you please help .

Here is my form.py file this my form file where I have form and validation .

django import forms
from django.core import validators
from new_app.models import Names``
from django.shortcuts import render
from django.http import HttpResponse



class Forms(forms.ModelForm):


FirstName = forms.CharField(label='FirstName',widget=forms.TextInput(attrs={"placeholder":"your Name","rows":20,"column":100}))

    class Meta():
        model = Names
        fields = '__all__'

    def clean(self):

        all_data = super().clean()
        firstname= all_data['FirstName']
        lastname = all_data['LastName']
        a = 1
        if firstname in lastname:
                raise forms.ValidationError("amazing")
        return all_data

Here is my view.py file this is my view file

from django.shortcuts import render
from new_app.models import Names
from new_app import formss
from django import forms

# Create your views here.
def index(request):
    a = Names.objects.all()
    dict = {'write':a}
return render(request , 'index.html', context = dict)

def register(request):

second = formss.Forms()


if request.method == 'POST':
    form = formss.Forms(request.POST)

    if form.is_valid():
        form.save(commit=True)
        return index(request)
    else:
        second = formss.Forms()






return render (request , 'forms.html', context = {'form':second} )

Here is my form.html template file ...

  <!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

    <title>Login or Sign up</title>
  </head>
  <body>
    
    <div class="container">
      <div class="jumbotron">
        <h1>         Sign up</h1>
      </div>

    </div>

    <div class="container">


      <div class="first">
        <form class="form-horizontal" method="post">
          {{form.as_p}}

          {% csrf_token%}
          <input type="submit" class="btn btn-primary" value="Submit">
        </form>


      </div>
    </div>

  </body>
</html>

1 Answer 1

1

You need add the error tags into the template in order to see the validation errors:

{% if form.errors %}
       {% for field in form %}
              {% for error in field.errors %}
                    <div class="alert alert-danger">
                          <strong>{{ error }}</strong>
                    </div>
              {% endfor %}
       {% endfor %}
       {% for error in form.non_field_errors %}
              <div class="alert alert-danger">
                    <strong>{{ error }}</strong>
              </div>
       {% endfor %}
{% endif %}

You also need to ensure the original completed form is being rendered, in order to show the errors.

def register(request):
    if request.method == 'GET':
        form = formss.Forms()

    if request.method == 'POST':
        form = formss.Forms(request.POST)

        if form.is_valid():
            form.save(commit=True)
            return index(request)

    return render (request , 'forms.html', context = {'form':form} )
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you so much ....So i can get error from form file to html directly?
When you load the form into the template it will check for the errors attribute in the template and if it has them it will load them in. The form.is_valid() will generate errors if it does not validate. As you load the form back into the template in the post if it fail it will render the errors with it.
I get Multiple error one of bootsrap and other of simple django validation error ? how to only get bootsrap once . And how to get errors specifically . Like if I want to show specific error for specific field and for specific type of error
appreciate your answer but little hard for me to understand the work flow . I dont understand is the errors is from form.py validation or from views.py where there is form.is_valid(). how it is going to template
Yes when you run the method is_valid() it will perform validation checked and create the errors attribute of the form. This can then be seen in your template.
|

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.