gpt4 book ai didi

python - Django self.cleaned_data 不工作

转载 作者:太空宇宙 更新时间:2023-11-04 08:17:29 24 4
gpt4 key购买 nike

我是技术新手,所以如果问题太简单,我提前道歉。

我正在使用 self.cleaned_data 获取用户输入的选定数据。它在调用 clean 时起作用,但在我的保存方法上不起作用。

这是代码

表格.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/

Just like the general field clean() method, above, this method should return the cleaned data, regardless of whether it changed anything or not.

简单的改变是

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

关于python - Django self.cleaned_data 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11696639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com