6 ms·
"The second blank ""is the empty space where the field value goes." I'm not sure I understand what you're saying here, but I don't think this is right. cleaned
by voyou 10y ago
"The second blank ""is the empty space where the field value goes."
I'm not sure I understand what you're saying here, but I don't think this is right. cleaned_data is a dictionary, and so the second argument to get is a default value to use if there is no value corresponding to the key. So, in your example, if there's no value for "formfield1" in cleaned_data, it would return a string with a space in.
This is probably not what you want to do. cleaned_data will always contain values for declared form fields, so the only way the default would be used is if you've made a mistake in specifying the key (for instance, if there's a typo and you've written 'formfeild1' instead of 'formfield1'). If you've made that kind of mistake, you don't want the program to continue using a single space instead of the valid data (you'll lose data) - you want the program to report that there's something wrong.
So I think you should probably use
form.cleaned_data['formfield1']
which will do the same thing if formfield1 is a real field name specified in the form, but will raise an exception if it isn't.
- vivekd 10y agoSorry typo, it should be the second blank is the empty space where the user input goes. I just tried your solution with cleaned_data["formfield1"] instead of cleaned_data("formfield1","") and it didn't work for me, it came back with the following error message when I submitted the form: >'builtin_function_or_method' object is not subscriptable I think you have to have the empty quotes after the form field and I think that's what's capturing the user input so here "form.cleaned_data("formfield1","") seems to be telling django the form name and then the second field is where the associated user input goes which is then passed to the model.
- vivekd 10y agowait just tried the code again, you were right, I don't need the second empty string. I was using [] with get and it wasn't working. so it seems the right way is form.cleaned_data.get('formfieldvalue') or form.cleaned_data[formfieldvalue'] if you don't want to use get
- chubuntu 10y agoThank you