ees*_*ein 1 python django django-forms
我是这项技术的新手,所以如果问题太简单,我会提前道歉.
我正在使用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()
Run Code Online (Sandbox Code Playgroud)
当我打电话保存时,为什么不能正常工作?
这是我的views.py
def SignUp(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
Run Code Online (Sandbox Code Playgroud)
提前致谢.
该clean_<field_name表格上的方法必须返回清洁值或养ValidationError.来自文档https://docs.djangoproject.com/en/1.4/ref/forms/validation/
就像上面的常规字段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
Run Code Online (Sandbox Code Playgroud)