I am running into errors when trying to pass a Flask Session variable to WTForms. I have read everything about the topic through searches and documentation but the correct method is not clear to me. I am completely new to web development and I am sure there are certain concepts that I don't yet understand.
users_department is a Flask session variable that is used for populating a WTForm that lists the project_lead in a wtforms.SelectField. However, I receive a error when calling a Flask session variable, RunTimeError: working outside of request context.
Below is my code.
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()])