viernes, 24 de febrero de 2017

[Flask] - TypeError: 'NoneType' object is not iterable

Problema:


Flask reporta el error:

TypeError: 'NoneType' object is not iterable

Al hacer POST en un formulario Flask-WTF. La vista que la maneja es:

def nuevo_proyecto():
    newprojectform = NewProjectForm()
    if newprojectform.validate_on_submit():
        return redirect('/')
    else:
        tutors = User.query.filter(User.id != current_user.id)
        newprojectform.tutor.choices = [(t.id, t.username) for t in tutors]
        newprojectform.line.choices = [(l.id, l.nombreLinea) for l in lines]
        return render_template('nuevo_proyecto.html', lines=lines, newprojectform = newprojectform)


Solución:


Hay que definir choices antes de validar el formulario:

def nuevo_proyecto():
    newprojectform = NewProjectForm()
    tutors = User.query.filter(User.id != current_user.id)
    newprojectform.tutor.choices = [(t.id, t.username) for t in tutors]
    newprojectform.line.choices = [(l.id, l.nombreLinea) for l in lines]
    if newprojectform.validate_on_submit():
        return redirect('/')
    else:
        return render_template('nuevo_proyecto.html', lines=lines, newprojectform = newprojectform)

Funciona correctamente!


No hay comentarios:

Publicar un comentario