烧瓶wtf-forms SelectField不断失败

时间:2019-07-25 02:05:17

标签: python flask flask-wtforms

我有一个创建SelectField选择器的函数,但是无论我做什么,它总是会失败。我看到了几篇关于类似问题的文章,但我的情况都不是。 这里的forms.py

# ...
def select_project( proj_dict ):
    class SelectProject( FlaskForm ):
        new_project_name = SelectField(
            'Select Project',
            choices = list(proj_dict.items()) )
        submit = SubmitField('Change')
    return SelectProject()
# ...

,这里是routes.py

# ...
from app.forms import select_project

@app.route('/change_project', methods=['GET', 'POST'])
@login_required
def change_project():
    ### prendo una lista di tutti i progetti dell utente
    user = User.query.filter_by(username = current_user.username).first()
    all_projects = user.projects.all()
    proj_dict = {}
    for p in all_projects :
        proj_dict.update({ p : p })
    form = select_project(proj_dict)
    if form.validate_on_submit():
        flash( form.new_project_name.data )
        proj = Project( name = form.new_project_name.data )
        user.actual_proj = form.new_project_name.data
        proj.last_access = datetime.datetime.utcnow()
        db.session.commit()
        return redirect(url_for('project_homepage') )
    return render_template("change_project.html.j2", title='Change Project', form = form )
# ...

无论我尝试什么,它都会继续失败:

 [Not a valid choice] 

在此先感谢您的帮助!

0 个答案:

没有答案