1

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

3
  • Did your clean() method get called during the execution? Commented May 2, 2020 at 18:35
  • @ArakkalAbu Yes, I checked it with print. Its working both emails are not matching but validation Error is not coming in template Commented May 2, 2020 at 18:37
  • Is there any problem witn return render method.. in views.py, Commented May 2, 2020 at 18:38

1 Answer 1

1

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})
Sign up to request clarification or add additional context in comments.

Comments

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.