Django error: 'NoneType' object is not subscriptable

16,000

Solution 1

Your clean method in the forms.py should return the self.cleaned_data, if it doesn't raise an error.

Currently, it is returning None (as you are not returning anything explicitly)

Solution 2

Going by the contents of this line:

newuser = User(username=rf.cleaned_data['username'],email=rf.cleaned_data['email'])

it seems to me that the form instance rf doesn't have a cleaned_data field. The 'NoneType' object is unsubscriptable error can be raised when you are trying to access cleaned_data like you would access a dictionary but cleaned_data is actually None.

To check (rather clumsily) add a print statement before the offending line:

print rf.cleaned_data

OK. On closer look this is the most likely culprit:

def clean(self):
    if self.cleaned_data['cpassword']!=self.cleaned_data['password']:
        raise forms.ValidationError("Passwords don't match")

Clean methods should return cleaned_data explicitly. Since this one is not, it causes an error.

Share:
16,000
JPC
Author by

JPC

Updated on July 13, 2022

Comments

  • JPC
    JPC almost 2 years

    It's taking me way to long to make this simple form. Almost there but when I submit I get the NoneType error

    views.py:

    from djangoproject1.authentication import forms
    from django.contrib.auth.models import User
    from django.http import HttpResponseRedirect
    from django.shortcuts import render_to_response
    
    def main(request):
        rf = forms.RegisterForm()
        pf = forms.ProfileForm()
        return render_to_response("authentication/index.html", {'form1': rf, 'form2':pf})
    
    def register(request):
        if request.method == 'POST':
            rf = forms.RegisterForm(request.POST)
            pf = forms.ProfileForm(request.POST)
            if rf.is_valid() and pf.is_valid():
                newuser = User(username=rf.cleaned_data['username'],email=rf.cleaned_data['email']) # this is the offending line
                newuser.set_password(rf.cleaned_data['password'])
                newuser.save()
                profile = pf.save(commit=False)
                profile.user = newuser
                profile.save()
                return HttpResponseRedirect("/register-success/")
        else: 
            return main(request)
    

    forms.py:

    from django import forms
    from djangoproject1.authentication.models import UserProfile    
    
    class RegisterForm(forms.Form):
        username = forms.CharField(min_length=6,max_length=15)
        password = forms.CharField(min_length=6,max_length=15,widget = forms.PasswordInput())
        cpassword = forms.CharField(label='Confirm Password',widget = forms.PasswordInput())
        email = forms.EmailField(label='E-mail Address')
    
        def clean(self):
            if self.cleaned_data['cpassword']!=self.cleaned_data['password']:
                raise forms.ValidationError("Passwords don't match")
    
    class ProfileForm(forms.ModelForm):
        phonenumber = forms.CharField(label='Phone Number')
    
        class Meta:
            model = UserProfile
            exclude = ('user')
    

    Stack trace:

    Environment:
    
    Request Method: POST
    Request URL: http://localhost:8000/register/
    Django Version: 1.2.1
    Python Version: 2.7.0
    Installed Applications:
    ['django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'djangoproject1.authentication']
    Installed Middleware:
    ('django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware')
    
    
    Traceback:
    File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
      100.                     response = callback(request, *callback_args, **callback_kwargs)
    File "C:\Users\jec23\My Java Projects\djangoproject1\src\djangoproject1\..\djangoproject1\authentication\views.py" in register
      21.             newuser = User(username=rf.cleaned_data['username'],email=rf.cleaned_data['email'])
    
    Exception Type: TypeError at /register/
    Exception Value: 'NoneType' object is not subscriptable