Search code examples
pythondjangodjango-viewsdjango-formskeyerror

Django KeyError at forms.py


I'm learning django and I'm playing around with forms and registering users. When I submit the registration form with the username, email, password, and confirm password fields filled, I get an error that states:

KeyError at director/register/ "confirm_password"

However, when I look at the POST data in the error message I clearly see the data I'm trying to access.

[see screenshot here](https://i.sstatic.net/S19QH.png)

csrfmiddlewaretoken: blahblahblabh

username: dasdasd

email: [email protected]

password: test123!

confirm_password: test123!

Below are the forms.py, portion of the views.py, register.html files.

forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm

from .models import CustomUser

class CustomUserCreationForm(UserCreationForm):

    class Meta:
        model = CustomUser
        fields = ("email", "password")

class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        fields = ("email", "password")

class RegisterUser(forms.Form):
    username = forms.CharField(max_length=64, required=True)
    email = forms.EmailField()
    password = forms.CharField(max_length=128, widget=forms.PasswordInput(), required=True)
    confirm_password = forms.CharField(max_length=128, widget=forms.PasswordInput(), required=True)

    def clean_username(self):
        username = self.cleaned_data["username"]
        if CustomUser.objects.filter(username=username).exists():
            raise forms.ValidationError("Email already exists")
        return username
    
    def clean_email(self):
        email = self.cleaned_data['email']
        if CustomUser.objects.filter(email=email).exists():
            raise forms.ValidationError("Email already exists")
        return email

    def clean_password(self):
        password = self.cleaned_data['password']
        confirm_password = self.cleaned_data['confirm_password']
        if password != confirm_password:
            raise forms.ValidationError("Passwords do not match")
        return password```

views.py (register_view)

def register_view (request):
    if request.method == "POST":
        form = RegisterUser(request.POST)
        if form.is_valid():
            new_user = form.save()
            login(request, new_user)
            return redirect('home')
    else:
        form = RegisterUser()
    return render(request, 'pokedex/register.html', {'form': form})

register.html

{% extends 'pokedex/layout.html' %}

{% block title %}Register{% endblock title %}


{% block body %}

<h1>Register</h1>

{% if form.errors %}
<div class="alert alert-danger">
    <ul>
        {% for error in form.errors.values %}
            <li>{{ error }}</li>
        {% endfor %}
    </ul>
</div>
{% endif %}

<form action="{% url 'register' %}" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Register">
</form>

{% endblock body %}

EDIT: Added Traceback

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/pokedex/register/

Django Version: 4.1.7
Python Version: 3.9.2
Installed Applications:
['pokedex',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Users\Alice\Documents\Code\env\lib\site-packages\django\core\handlers\exception.py", line 56, in inner
    response = get_response(request)
  File "C:\Users\Alice\Documents\Code\env\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Alice\Documents\Code\stepchallenge\pokedex\views.py", line 45, in register_view
    if form.is_valid():
  File "C:\Users\Alice\Documents\Code\env\lib\site-packages\django\forms\forms.py", line 205, in is_valid
    return self.is_bound and not self.errors
  File "C:\Users\Alice\Documents\Code\env\lib\site-packages\django\forms\forms.py", line 200, in errors
    self.full_clean()
  File "C:\Users\Alice\Documents\Code\env\lib\site-packages\django\forms\forms.py", line 437, in full_clean
    self._clean_fields()
  File "C:\Users\Alice\Documents\Code\env\lib\site-packages\django\forms\forms.py", line 452, in _clean_fields
    value = getattr(self, "clean_%s" % name)()
  File "C:\Users\Alice\Documents\Code\stepchallenge\pokedex\forms.py", line 38, in clean_password
    confirm_password = self.cleaned_data['confirm_password']

Exception Type: KeyError at /pokedex/register/
Exception Value: 'confirm_password'

Solution

  • As described in the Django official documentation in the section Forms at Cleaning and validating fields that depend on each other, you cannot read self.cleaned_data['confirm_password'] inside the function clean_password, as the field might not be populated yet.

    I suggest you to do the validation overriding the clean function like this:

    class RegisterForm(forms.Form):
        username = forms.CharField(max_length=64, required=True)
        email = forms.EmailField()
        password = forms.CharField(max_length=128, widget=forms.PasswordInput(), required=True)
        confirm_password = forms.CharField(max_length=128, widget=forms.PasswordInput(), required=True)
    
        def clean_username(self):
            username = self.cleaned_data["username"]
            if CustomUser.objects.filter(username=username).exists():
                raise forms.ValidationError("Email already exists")
            return username
        
        def clean_email(self):
            email = self.cleaned_data['email']
            if CustomUser.objects.filter(email=email).exists():
                raise forms.ValidationError("Email already exists")
            return email
        
        # Removed clean_password function to use the default validation
        
        # Moved password check after all field are cleaned
        def clean(self):
            cleaned_data = super().clean()
            password = cleaned_data['password']
            confirm_password = cleaned_data['confirm_password']
            if password != confirm_password:
                raise forms.ValidationError("Passwords do not match")
    

    I suggest also to take a look to Django official documentation - Forms as it describes how to avoid most errors.