I'm new to the technology, so I apologyze in advance if the question is too simple.
I'm using self.cleaned_data to get the selected data entered by the user. And it works when clean is called, but not on my save method.
Here is the code
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()
Any ideas why that's not working when I call save?
Here is my views.py
def SignUp(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
Thanks in advance.
The clean_<field_name
methods on the form must return the clean value or raise a ValidationError
. From the docs 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.
The simple change would be
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.