我正在django做一个小密码确认表。但我很困惑,因为self.cleaned_data.get('confirm_password')
总是返回任何一个,因此我的密码永远不会相互匹配。
继承clean
forms.py
方法
def clean_password(self):
password = self.cleaned_data.get("password")
confirm_password = self.cleaned_data.get("confirm_password")
if password != confirm_password:
print(password)
print(confirm_password)
raise forms.ValidationError(
"Password and password confirmation does not match"
)
return password
self.cleaned_data.get('password')
返回键入的密码,但confirm_password不会。
在清除前看到收到的数据的view.py
中,confirm_password显示为
......................user = UserRegister(request.POST)
print(request.POST.get('confirm_password'))
if user.is_valid():
print('valid')..........................
这可能是什么原因?
见forms.py
first_name = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={'placeholder': 'First Name'}))
last_name = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={'placeholder': 'Last Name'}))
username = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={'placeholder': 'Username'}))
email = forms.EmailField( required=True, widget=forms.widgets.EmailInput(attrs={'placeholder': 'Email'}))
password =forms.CharField(required=True, widget=forms.widgets.PasswordInput())
confirm_password =forms.CharField(required=True, widget=forms.widgets.PasswordInput())
class Meta:
model=User
fields=['first_name','last_name','username','email','password','confirm_password']
答案 0 :(得分:2)
clean_fieldname
方法按字段声明的顺序调用,因此调用clean_password
时cleaned_data
不包含confirm_password
。您可以使用clean_confirm_password
方法执行此验证:
def clean_confirm_password(self):
password = self.cleaned_data.get("password")
confirm_password = self.cleaned_data.get("confirm_password")
if password != confirm_password:
print(password)
print(confirm_password)
raise forms.ValidationError(
"Password and password confirmation does not match"
)
return password
或仅使用clean
方法进行验证,需要访问多个表单字段。
答案 1 :(得分:2)
在验证字段时调用//Thursday and Friday will be excluded as weekend.
var start = this.getField("From").value;
// get the start date value
var end = this.getField("To").value;
var end = util.scand("dd/mm/yyyy H:MM:SS", end + " 0:00:00");
var start =util.scand("dd/mm/yyyy H:MM:SS", start + " 0:00:00");
event.value = dateDifference(start, end);
function dateDifference(start, end) {
// Copy date objects so don't modify originals
var s = new Date(+start);
var e = new Date(+end);
// Set time to midday to avoid daylight saving and browser quirks
s.setHours(12,0,0,0);
e.setHours(12,0,0,0);
// Get the difference in whole days
var totalDays = Math.round((e - s) / 8.64e7);
// Get the difference in whole weeks
var wholeWeeks = totalDays / 7 | 0;
// Estimate business days as number of whole weeks * 5
var days = wholeWeeks * 5;
// If not even number of weeks, calc remaining weekend days
if (totalDays % 7) {
s.setDate(s.getDate() + wholeWeeks * 7);
while (s < e) {
s.setDate(s.getDate() + 1);
// If day isn't a Thursday or Friday, add to business days
if (s.getDay() != 4 && s.getDay() != 5) {
++days;
}
}
}
var hdayar = ["2018/02/21","2018/03/17","2018/03/26","2018/04/14","2018/05/01","2018/08/15","2018/09/2 1","2018/10/18","2018/10/19","2018/12/16","2018/12/25"];
//test for public holidays
var phdays = 0;
for (var i = 0; i <hdayar.length; i++){
if ((Date.parse(hdayar[i]) >= Date.parse(start)) && (Date.parse(hdayar[i]) <= Date.parse(end))) {phdays ++;}}
return days-phdays + 1;
}
方法。对于您正在使用的字段,您不能指望当时在cleaning_data中的任何其他字段。如果需要使用多个字段覆盖表单的clean()方法。
答案 2 :(得分:2)
由于您在多个字段之间进行验证,因此您应该覆盖表单的clean
方法。
def clean(self):
cleaned_data = super().clean()
password = self.cleaned_data.get("password")
confirm_password = self.cleaned_data.get("confirm_password")
if password != confirm_password:
print(password)
print(confirm_password)
raise forms.ValidationError(
"Password and password confirmation does not match"
)
return cleaned_data