[英]Django self.cleaned_data not working
我是該技術的新手,所以如果問題太簡單,我會提前道歉。
我正在使用self.cleaned_data獲取用戶輸入的所選數據。 它在調用clean時起作用,但不適用於我的save方法。
這是代碼
Forms.py
def clean_account_type(self):
if self.cleaned_data["account_type"] == "select": # **here it works**
raise forms.ValidationError("Select account type.")
def save(self):
acc_type = self.cleaned_data["account_type"] # **here it doesn't, (NONE)**
if acc_type == "test1":
doSomeStuff()
有什么想法為什么在我調用保存時不起作用?
這是我的views.py
def SignUp(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
提前致謝。
表單上的clean_<field_name
方法必須返回凈值或引發ValidationError
。 從文檔https://docs.djangoproject.com/en/1.4/ref/forms/validation/
就像上面的常規field clean()方法一樣,此方法應返回已清除的數據,而不管其是否更改了任何內容。
簡單的變化是
def clean_account_type(self):
account_type = self.cleaned_data["account_type"]
if account_type == "select":
raise forms.ValidationError("Select account type.")
return account_type
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.