所以我在将一个列表从我的 Flask 会话对象传递到我正在使用的 WTForms 时遇到了问题。我正在尝试访问会话对象中的“策略”对象(它是一个列表)。
目标是列出与当前用户关联的所有策略。
在 Flask 应用程序中:
class Create_Indicator_Form(Form):
security = TextField('Ticker Name', [
validators.Required(),
validators.Length(min=1, max=6)])
mva_10 = BooleanField('10 day moving average')
mva_25 = BooleanField('25 day moving average')
strategy = SelectField('strategy', session['strategy'])
def validate_mva_10(form, field):
if form.mva_25.data is True and field.data is True:
raise ValidationError('You can only choose one reference')
if form.mva_25.data is False and field.data is False:
raise ValidationError('You must choose at least one reference')
@app.route('/create_indicator', methods=['GET', 'POST'])
def create_indicator():
check_if_logged_in()
f = request.form
create_indicator_form = Create_Indicator_Form(f)
if request.method == 'POST' and create_indicator_form.validate():
indicator_id = get_next_index('indicator', 'indicator_id')
ticker = create_indicator_form.security.data
if create_indicator_form.mva_10.data is True:
mva_10_day = 'Y'
mva_25_day = 'N'
else:
mva_10_day = 'N'
mva_25_day = 'Y'
row = [indicator_id, ticker, mva_10_day, mva_25_day]
add_data('indicator', row)
# adding relation
criteria_row = [session['strategy'][0], indicator_id]
add_data('criteria', criteria_row)
return redirect(url_for('home'))
create_indicator_form.strategies = session['strategy']
return render_template('create_indicator.html',
form=create_indicator_form)
当我尝试运行烧瓶应用程序时,我抛出了这个错误:
RuntimeError: working outside of request context
回溯到我在 Create_Indicator_Form 类中访问会话对象的位置。
自从开始尝试解决此问题以来,我已经意识到使用选择字段在 mva_10 和 mva_25 之间进行选择会更有效,但我想在重构之前解决此问题。