FlaskWTForms SelectField获取当前所选项目

2024-10-01 13:36:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难在提交时在一个烧瓶应用程序的WTForms页面中获取当前选择的项目。这个表格选择器数据无论在SelectField中选择了哪个项,value在提交时总是等于1(所有选项都有一个从1到10的惟一表id,格式为(1,'table_name'),其中1是一个整数。在

以下是当前代码:

在视图.py在

from flask import session

def view(self):
    form = Tableset(request.form)
    if request.method == 'POST' and form.validate():
        #form.tableselector.data always returns 1 no matter which item is selected?
        session['table_id'] = form.tableselector.data

在表单.py在

^{pr2}$

在表格视图.html在

<form role="form" action="{{ url_for('app.table_view') }}" method="post">
    {{ form.csrf_token }}
    <div class="form-group">
    {{ form.tableselector.label }} 
    {{ form.tableselector }}
    </div>

我注意到在调试表单时,wtf Meta object csrf属性等于True,所以我知道csrf正在工作。在


Tags: pydivformview视图id表单data