The cleaned_data in django is missing data.

write a user registration with djang, passing four parameters in req.POST

clipboard.png
formdata

clipboard.png

form.is_valid()
self.cleaned_dataenpassword,self.data

clipboard.png

Why
the code is as follows:
forms.py

from django import forms
class UserForm(forms.Form):
    email=forms.EmailField(label="")
    username=forms.CharField(max_length=20,label="")
    password=forms.CharField(max_length=20,label="")
    enpassword=forms.CharField(max_length=20,label="")
    def clean_password(self):
        password=self.cleaned_data["password"]
        enpassword=self.cleaned_data["enpassword"]
        if password==enpassword:
            return password
        else:
            raise forms.ValidationError("Please re-enter your password.")

view.py

def registe_view(req):
    if req.method=="POST":
        form=UserForm(req.POST)
        if form.is_valid():
            username=form.cleaned_data["username"]
            email=form.cleaned_data["email"]
            password=form.cleaned_data["password"]
            enpassword=form.cleaned_data["enpassword"]
            user=authenticate(username=username,email=email,password=password)
            user=User.objects.create_user(username=username,email=email,password=password)
            user.save()
            context={"title":"","status":""}
            return render(req,"redirect.html",context=context)
    else:
        form=UserForm()
    return render(req,"login/registe.html",context={"form":form,"value":""})

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ value }}</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {% for field in form %}
        <p class="fieldWrapper">
            {{ field.label_tag }}{{ field }}
            {{ field.errors }}
        

{% endfor %} <input type="submit" value="{{ value }}"> </form> </body> </html>
Mar.03,2021

change clean_enpassword, to clean will not jump prompt message, I don't know why


After looking at it for a long time, I can't see what's wrong with it. 233.
also doesn't see where you execute the clean_password class function.

  

excuse me, have you solved it? I have the same problem. I only get the value of password. I'm in a hurry without the value of username

.
Menu