简体   繁体   中英

Form object not iterable in Django

I want to assign the input of a form to a function in my view but I keep getting this error. Please help do I fix it.

Error

receiver = list(ToolsForm.declared_fields['receiver_mail'])
TypeError: 'CharField' object is not iterable

You can obtain the value associated with the receiver_mail field with:

receiver = form.cleaned_data['receiver_mail']

so without using list(…) part and with .cleaned_data [Django-doc] , the form should be an instance of ToolForm , not a reference to the ToolForm class. Before you can retrieve the data, you will first need to validate the form, so:

form = ToolsForm(request.POST, request.FILES)
if form.is_valid():
    receiver = form.cleaned_data['receiver_mail']

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM