1
votes

from django import forms from django.core import validators

class FormName(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()
    verify_email = forms.EmailField(label = "enter your email Again")
    text  = forms.CharField(widget = forms.Textarea)

    def clean(self):
        all_clean_data = super().clean()
        email = all_clean_data['email']
        vmail = all_clean_data['verify_email']

        if email != vmail:
            raise forms.ValidationError("Error i Email matching")

views.py

from django.shortcuts import render
from . import form

# Create your views here.

def index(request):
    return render(request,'basicapp/index.html')


def form_name_view(request):
    forms = form.FormName()

    if request.method == "POST":
        formObject  = form.FormName(request.POST)

        if formObject.is_valid():
            print("Sucess!!!!!")
            print(formObject.cleaned_data['name'])
            print(formObject.cleaned_data['email'])
            print(formObject.cleaned_data['text'])



    return render(request,'basicapp/form_page.html',{'form':forms})

form_page.html

<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Forms</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  </head>
  <body>
    <div class = "container">
      <h1>Fill out the form</h1>
      <form method="POST">
        {{form.as_p}}
        {% csrf_token %}
        <input type="submit" class="btn btn-primary" value = "Submit">
      </form>
    </div>
  </body>
</html>

I am Not sure What I am missing,

I have done everything and had done enough research, But could not find the solution.

Am I missing something because of the versioning of django.

I am following one udemy course and didn't get response, Thats y I am posting here.

Thanks in advance

1
Did your clean() method get called during the execution?JPG
@ArakkalAbu Yes, I checked it with print. Its working both emails are not matching but validation Error is not coming in templateKrishna
Is there any problem witn return render method.. in views.py,Krishna

1 Answers

1
votes

The issue was in your views, you were not rendering the form object properly. try this,

def form_name_view(request):
    if request.method == "POST":
        formObject = form.FormName(request.POST)

        if formObject.is_valid():
            print("Sucess!!!!!")
            # do some redirection
    else:
        # if a GET (or any other method) we'll create a blank form
        formObject = form.FormName()
    return render(request, 'basicapp/form_page.html', {'form': formObject})