Reputation: 9850
I'm getting the following error when trying to submit a form:
argument of type 'NoneType' is not iterable
My form class is as follows:
class requestform(ModelForm):
def __init__(self, *args, **kwargs):
super(requestform, self).__init__(*args,**kwargs)
self.is_update=False
requestedpart = forms.CharField (label="Requested Part", max_length=80, required= True)
librarytype = forms.TypedChoiceField (label="Type", choices = ((1, "Type1"), (2, "Type2"), (3, "Type3")), coerce = lambda x: bool(int(x)), widget = forms.RadioSelect, required= True)
requestformat = forms.TypedChoiceField (label="Format", choices = ((1, "Format1"), (2, "Format2")), coerce = lambda x: bool(int(x)), widget = forms.RadioSelect, required= True)
def clean(self):
if 'requestedpart' not in self.cleaned_data:
return
if not self.is_update:
return self.cleaned_data
class Meta:
model = LibraryRequest
My views is as follows
if request.method == "POST":
formtoaddrequest = requestform(request.POST, request.FILES)
if requestform.is_valid(formtoaddrequest):
new_request = formtoaddrequest.save(commit=False)
new_request.requestadder = request.user
new_request.save()
request.user.message_set.create(message=_("You have saved '%(requestedpart)s'") % {'requestedpart': new_request.requestedpart})
Here's my traceback
Traceback:
File "/Users/AB/Desktop/myenv2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/AB/Desktop/store/project/views.py" in view_requests
201. if formtoaddmodel.is_valid():
File "/Users/AB/Desktop/myenv2/lib/python2.7/site-packages/django/forms/forms.py" in is_valid
121. return self.is_bound and not bool(self.errors)
File "/Users/AB/Desktop/myenv2/lib/python2.7/site-packages/django/forms/forms.py" in _get_errors
112. self.full_clean()
File "/Users/AB/Desktop/myenv2/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
269. self._post_clean()
File "/Users/AB/Desktop/myenv2/lib/python2.7/site-packages/django/forms/models.py" in _post_clean
316. self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude)
File "/Users/AB/Desktop/myenv2/lib/python2.7/site-packages/django/forms/models.py" in construct_instance
40. or not f.name in cleaned_data:
Exception Type: TypeError at /project/view_requests/
Exception Value: argument of type 'NoneType' is not iterable
Any insight into why I might be getting this error?
Upvotes: 6
Views: 7256
Reputation: 53386
There are multiple issues.
Your clean method should either raise a ValidationError
or return self.cleaned_data
. Your clean()
method does not raise error but returns None
, change is to as below:
def clean(self):
if self.cleaned_data and 'requestedpart' not in self.cleaned_data:
raise forms.ValidationError("Some error message")
if not self.is_update:
return self.cleaned_data
return self.cleaned_data
Similarly, if you have clean_fieldname
for a field fieldname
, return its value:
def clean_fieldname(self):
# ...
return self.cleaned_data['fieldname']
You can also check if self.cleaned_data
is non-null
In your view, form validation should be like: if requestform.is_valid()
ie. without parameters.
Upvotes: 18
Reputation: 600059
You shouldn't have an empty return
in your clean
method. Either return cleaned_data if the form is valid, or raise a forms.ValidationError
if it's not.
Upvotes: 4