Django Form : cleaned_data.get(field name) is returning None in clean() method
I am following the Django documentation of Django form but unable to understand what is the issue in my code. I am writing the below code in the clean method to check if both name and email starts with lowercase s or not but Django is returning None in cleaned_data.get(field name) method and I am getting "Attribute error" : 'NoneType' object has no attribute 'startswith'. Please help me on this: Reference: https://docs.djangoproject.com/en/4.1/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other
from django import forms
from django.core.exceptions import ValidationError
class GirlsFeedback(forms.Form):
name = forms.CharField(label = 'Enter Your Name', label_suffix = " ", required=True, disabled=False, min_length = 5, max_length = 100, strip=True)
password = forms.CharField(label='Enter Your Password', label_suffix = " ", required=True, disabled=False,
min_length=8, max_length=10, help_text="Minimum 8 and Maximum 10 characters are allowed.", widget=forms.PasswordInput)
email = forms.EmailField(error_messages={'required': 'Email is mandatory'})
def clean(self):
cleaned_data = super().clean()
name = cleaned_data.get('name')
email = cleaned_data.get('email')
if name.startswith('s') and email.startswith('s') !=True:
raise ValidationError('Name and email both should start with a lowercase s')
Error:
AttributeError at /feedback3/
'NoneType' object has no attribute 'startswith'
Request Method: POST
Request URL: http://localhost:8000/feedback3/
Django Version: 4.1.2
Exception Type: AttributeError
Exception Value:
'NoneType' object has no attribute 'startswith'
Exception Location: C:\Users\singh\Desktop\Journey\Django Journey\Geeky
Shows\Eleven\Feedback3\forms.py, line 72, in clean
Raised during: Feedback3.views.feedback
Python Executable: C:\Users\singh\AppData\Local\Programs\Python\Python310\python.exe
Python Version: 3.10.7
It seems that form is not receiving any data, that's why it returned NoneType, although you can use default value to prevent error as:
def clean(self):
cleaned_data = super().clean()
name = cleaned_data.get('name', "Sujit Singh")
email = cleaned_data.get('email', "sujit123@gmail.com")
if name.startswith('s') and email.startswith('s') !=True:
raise ValidationError('Name and email both should start with a lowercase s')
Currently it will return default values instead of NoneType so It will not give any errors.
The view should be:
def feedback(request):
if request.method == 'POST':
var = GirlsFeedback(request.POST)
if var.is_valid():
name = var.cleaned_data['name']
password = var.cleaned_data['password']
email = var.cleaned_data['email']
return HttpResponseRedirect('/feedback3/success')
else:
var = GirlsFeedback()
return render(request, 'Feedback3/feedback.html', {'data': var})