Why i am getting Validation error even when input is correct in form
I was learning Django form validation from some online resource , was working on form validation , I created a validation error depicting the condition and passed it in form field , but the validation error is raising even if I am giving correct input. Similar to this happened when i raised a validation error for botcatcher without using validators, the error was not raised even i was giving input through inspect (its code is commented) ,Do help me out!
forms.py
from django import forms
from django.core import validators
def check_name(value):
if value[0].lower() != 'K':
raise forms.ValidationError('Name should start with K')
class FormName(forms.Form):
name = forms.CharField(validators=[check_name])
email = forms.EmailField()
text = forms.CharField(widget=forms.Textarea)
botcatcher = forms.CharField(required=False,
widget=forms.HiddenInput,
validators=[validators.MaxLengthValidator(0)])
# def cleaned_botcatcher(self):
# botcatcher = self.cleaned_data['botcatcher']
# if len(botcatcher) > 0:
# raise forms.ValidationError('CAUGHT THE BOT')
# return botcatcher
[webpage for same][1]
views.py
from django.shortcuts import render
from . import forms
# we can also use from basicapp import forms . refers to current directory
# Create your views here.
def index(request):
return render(request, 'basicapp/index.html')
def form_name_view(request):
form = forms.FormName()
if request.method == 'POST':
form = forms.FormName(request.POST)
if form.is_valid():
# DO something
print("VALIDATION SUCCESS , POSTED")
# Retreiving posted data
print("NAME: " + form.cleaned_data["name"])
print("EMAIL: " + form.cleaned_data["email"])
print("TEXT: " + form.cleaned_data["text"])
return render(request, 'basicapp/form_page.html', {'form': form})
I did a mistake in check_name validator function there i have used lower but i am checking with upper case 'K' which is leading to unmatching.
def check_name(value):
if value[0].lower() != 'k':
raise forms.ValidationError('Name should start with k')
But still i need help regarding the botcatcher validator that was not raising the error , even when it should.