www.example.com上的Django KeyErrorforms.py

jmo0nnb3  于 2023-05-19  发布在  Go
关注(0)|答案(1)|浏览(158)

我正在学习django,我正在玩表单和注册用户。当我提交注册表单并填写用户名、电子邮件、密码和确认密码字段时,我收到一个错误,指出:
KeyError at director/register/ "confirm_password"
然而,当我查看错误消息中的POST数据时,我清楚地看到了我试图访问的数据。

csrfmiddlewaretoken: blahblahblabh
username: dasdasd
email: dasd@dsada.com
password: test123!
confirm_password: test123!
下面是forms.py的一部分views.py,register.html文件。
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 %}

编辑:添加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'
wwtsj6pe

wwtsj6pe1#

Forms一节中的Django official documentation中所述,您无法在函数clean_password中读取self.cleaned_data['confirm_password'],因为该字段可能尚未填充。
我建议你像这样覆盖clean函数进行验证:

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")

我建议也看看Django official documentation - Forms,因为它描述了如何避免大多数错误。

相关问题