Jhon Jhony
Jhon Jhony

Reputation: 15

Django not displaying Validationerror

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>

Upvotes: 0

Views: 82

Answers (1)

Lewis
Lewis

Reputation: 2798

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} )

Upvotes: 1

Related Questions