Home > Mobile >  Django Form : cleaned_data.get(field name) is returning None in clean() method
Django Form : cleaned_data.get(field name) is returning None in clean() method

Time:11-17

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

views.py:

from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import GirlsFeedback

# Create your views here.

def success(request):
    return render(request, 'Feedback3/success.html', {'name':name,'email':email})

def feedback(request):
    if request.method == 'POST':
        var = GirlsFeedback(request.POST)
        if var.is_valid():
            global name, password, email 
            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})

feedback.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Feedback 3</title>
</head>
<body>
    <h1>This is the third feedback page</h1>
    <form action="" method="POST" novalidate>
        {% csrf_token %}
        
        {{data.as_p}}

        <input type="submit" value="Submit Data">

    </form>
</body>
</html>

Success.html:

<!DOCTYPE html>
<html lang="en">


<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Feedback3</title>
</head>


<body>
    <p>Dear {{name}}, Thanks for your feedback. Your feedback has been submitted with the below details </p>
    <p>Name : {{name}}</p>
    <p>Email : {{email}}</p>
</body>


</html>

CodePudding user response:

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', "[email protected]")
    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})
  • Related