2017-01-02 13 views
1

提出時にFlaskアプリのWTFormsページで現在選択されている項目を取得するのが難しいです。 SelectFieldのどの項目が選択されていても、form.tableselector.dataの値は常に1になります(すべての選択肢は、フォーム1(1、 'table_name')の1〜10のユニークなテーブルIDを持ちます。 。整数Flask WTForms SelectField現在選択されている項目を取得

ここでは、現在のコードである:

views.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 

form.py

class MyBaseForm(Form): 
    #see https://wtforms.readthedocs.io/en/latest/csrf.html 
    class Meta: 
     csrf = True 
     csrf_class = SessionCSRF 
     csrf_secret = app.config.get('SECRET_KEY') 

     @property 
     def csrf_context(self): 
      return session 

class Tableset(MyBaseForm): 
    tableselector = SelectField(label = 'Table', choices = [], coerce=int, id='select_table') 
    submit = SubmitField('Submit') 

    def validate(self): 
     if len(self.tableselector.choices) == 0: 
      return False 
     return True 

tableview.html

wtfメタオブジェクトのcsrfプロパティがTrueと等しいので、CSRFが動作していることを知っています。

答えて

関連する問題