[英]Django forms difference, one works, the second one doesn't
I've got a kind strange problem.我有一个奇怪的问题。 I've got two different (but almost the same forms and validation).我有两个不同的(但几乎相同的 forms 和验证)。 The validation in first one works, in the second one doesn't.第一个验证有效,第二个验证无效。 Can someone tell me why?有人能告诉我为什么吗?
1) 1)
class SignUpForm1(forms.Form):
test_value = forms.CharField(
label='Your name',
max_length=100,
widget=forms.TextInput(attrs={'class': "input"})
)
def clean_test_value(self):
data = self.cleaned_data.get('test_value')
print(data)
if data != "abc":
raise forms.ValidationError("Error")
return data
class SignUpForm2(forms.Form):
name = forms.CharField(
label='name',
max_length=100,
widget=forms.TextInput(attrs={'class': "input"})
)
def clean_name_value(self):
data = self.cleaned_data.get('name')
print(data)
if data != "abc":
raise forms.ValidationError("Error")
return data
View:看法:
class SignUpView(View):
def get(self, request):
form = SignUpForm1() #or SignUpForm2()
return render(request, "index.html", {'form': form})
def post(self, request):
form = SignUpForm1(request.POST) #or SignUpForm2(request.POST)
if form.is_valid():
print('valid')
return render(request, "index.html", {"form": form})
And the html template:以及 html 模板:
enter code here<form action="" method="post" class="formField">
{% csrf_token %}
{{form}}
<button type="submit" class="btn-form">Test</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.