0
一個類的實例類方法誘發我無法理解爲什麼和如何RegistrationForm類方法的時候,我在我的view.py創建類的實例clean_username是誘發如何以及爲什麼在創建
我form.py
class RegistrationForm(forms.Form):
firm_name = forms.CharField()
username = forms.CharField()
def clean_username(self):
print "inside clean_username ..I don know how this called"
if db.firm.find(username =self.cleaned_data['username']):
raise forms.ValidationError((u'This username is already taken. Please choose another.'))
return self.cleaned_data['username']
我view.py
def main(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
new_user = form.save()
return HttpResponseRedirect("/thanks/")
else:
form = RegistrationForm()
return render(request, "registration.html", {
'form': form,
})
這會打印「clean_username ..我不要裏面知道這個所謂的」
根據我的理解類方法clean_username()不應該被運行,直到誘發像form.clean_username()
謝謝......當我直接調用方法時,我嚇壞了 –