write a user registration with djang, passing four parameters in req.POST
formdata
form.is_valid()
self.cleaned_dataenpassword,self.data
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>