问题描述
因此,正如标题所述,我必须自己填写表格才能将用户注册为学习任务。表单按预期工作,但是我想验证输入的数据(现在仅pwd1和pwd2匹配)。我写了一个干净的函数,但是它什么也不做,也没有报告错误。
form.py:
from django import forms
import phonenumbers
from phonenumber_field.formfields import PhoneNumberField
class RegisterForm(forms.Form):
email = forms.EmailField(
label='email address',max_length=255)
f_name = forms.CharField(max_length=30)
country = forms.CharField(max_length=30)
mobile = PhoneNumberField(region="IN")
address = forms.CharField(max_length=300)
pwd1 = forms.CharField(max_length=32,widget=forms.PasswordInput)
pwd2 = forms.CharField(max_length=32,widget=forms.PasswordInput)
def clean(self):
cleaned_data = super().clean()
pwd1 = cleaned_data.get("pwd1")
pwd2 = cleaned_data.get("pwd2")
if pwd1!= pwd2:
raise forms.ValidationError("Passwords don't match",code='password_mismatch',)
views.py
def register(request):
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
f_name = form.cleaned_data['f_name']
address = form.cleaned_data['address']
country = form.cleaned_data['country']
mobile = form.cleaned_data['mobile']
password = form.cleaned_data['pwd1']
pwd2 = form.cleaned_data['pwd2']
user = User.objects.create(
email=email,f_name=f_name,address= address,country= country,mobile= mobile,)
user.set_password(password)
user.save()
return redirect('/accounts/login/')
else:
return redirect('failure')
else:
form = RegisterForm
return render(request,'register.html',{'form':form})
return redirect('/register/')
register.html
<!DOCTYPE = 'html'>
<html>
{% load static %}
<link rel="stylesheet" href="{% static 'accounts/file.css' %}">
<head>
<div class="header">
<a href="/" class="logo">task1</a>
<div class="header-right">
<a href="/">Home</a>
{% if not request.user.login == True %}
<a class="active" href="/accounts/register/">Sign-up</a>
<a href="/accounts/login/">Sign-in</a>
{% endif %}
{% if request.user.login == True %}
<a href="/products/cart">Cart</a>
<a href="/accounts/logout/">Sign-out</a>
{% endif %}
</div>
</div>
</head>
<body>
<script type="text/javascript">
function matchpass(){
var firstpassword=document.f1.pwd1.value;
var secondpassword=document.f1.pwd2.value;
if(firstpassword==secondpassword){
return true;
}
else{
alert("password must be same!");
return false;
}
}
</script>
<form name="register",action="/accounts/register/",method='post',onsubmit="return matchpass()">
{% csrf_token %}
<br>
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.email.errors }}
<label for="{{ form.email.id_for_label }}">Email:</label><br>
{{ form.email }}
</div>
<br>
<div class="fieldWrapper">
{{ form.f_name.errors }}
<label for="{{ form.f_name.id_for_label }}">Full Name:</label><br>
{{ form.f_name }}
</div>
<br>
<div class="fieldWrapper">
{{ form.mobile.errors }}
<label for="{{ form.mobile.id_for_label }}">Mobile Number:</label><br>
{{ form.mobile }}
</div>
<br>
<div class="fieldWrapper">
{{ form.address.errors }}
<label for="{{ form.address.id_for_label }}">Address:</label><br>
{{ form.address }}
</div>
<br>
<div class="fieldWrapper">
{{ form.country.errors }}
<label for="{{ form.country.id_for_label }}">Country:</label><br>
{{ form.country }}
</div>
<br>
<div class="fieldWrapper">
{{ form.pwd1.errors }}
<label for="{{ form.pwd1.id_for_label }}">Password:</label><br>
{{ form.pwd1 }}
</div>
<br>
<div class="fieldWrapper">
{{ form.pwd2.errors }}
<label for="{{ form.pwd2.id_for_label }}">Confirm Password:</label><br>
{{ form.pwd2 }}
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)