尝试将Flask Session变量传递给WTForms时遇到错误。我通过搜索和文档阅读了有关该主题的所有内容,但我不清楚正确的方法。我对Web开发完全陌生,我确信有一些我还不了解的概念。
users_department是一个Flask会话变量,用于填充在wtforms.SelectField中列出project_lead的WTForm。但是,我在调用Flask会话变量RunTimeError时遇到错误:在请求上下文之外工作。
以下是我的代码。
import wtforms
import flask
def get_team_members():
user_department = flask.session.get('user_department')
# run SQLite query to find team members based on the variable user_department
# create a list from the SQLite results
return team_results
@app.route('/manage', methods=['GET', 'POST'])
def manage():
form = ProjectForm(request.form) #Call the project form to display
#The line below causes the error but it works if hard coded with a department
form.project_lead.choices = get_team_members() #Get team members for user
return render_template('manage.html', form = form)
class ProjectForm(wtforms.Form):
project_lead = wtforms.SelectField(u'Project Lead',
validators=[wtforms.validators.optional()])
答案 0 :(得分:1)
我想出了如何修复它。调用WTforms后不要调用烧瓶会话。以下是需要更改的部分。
@app.route('/manage', methods=['GET', 'POST'])
def manage():
team = get_team_members() #Get team members for user
form = ProjectForm(request.form) #Call the project form to display
form.project_lead.choices = team
return render_template('manage.html', form = form)