0

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()])

1 Answer 1

1

I figured out how to fix it. Do not call a flask session after calling WTforms. Below is the part that needed to change.

@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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.