2016-11-13 4 views
0

私はユーザーが希望するサブスクリプションを選択するための3つのオプションでRadioFieldを使用しています。そのフィールドの値は、そのユーザーのデータベースに保存されます。ユーザーが設定ページに戻ると、保存された値が選択されたラジオ・フィールドが表示されます。 これは私の現在のRadioFIeldです。フラスコWTFラジオボタンは、保存された値を表示します

subscription_tier = RadioField('Plan', choices=[(tier_one_amount, tier_one_string), 
(tier_two_amount, tier_two_string), (tier_three_amount, tier_three_string)], 
validators=[validators.Required()]) 

答えて

0

モデルにRadioFieldのデータを割り当てる必要があります。モデルはデータベースでもよいし、単純な辞書でもよい。ここではモデルとして辞書を使用するための簡単な例は次のとおりです。

from flask import Flask, render_template 
from wtforms import RadioField 
from flask_wtf import Form 

SECRET_KEY = 'development' 

app = Flask(__name__) 
app.config.from_object(__name__) 


my_model = {} 


class SimpleForm(Form): 
    example = RadioField(
     'Label', choices=[('value', 'description'), 
          ('value_two', 'whatever')] 
    ) 


@app.route('/', methods=['post','get']) 
def hello_world(): 
    global my_model 
    form = SimpleForm() 

    if form.validate_on_submit(): 
     my_model['example'] = form.example.data 
     print(form.example.data) 
    else: 
     print(form.errors) 

    # load value from model 
    example_value = my_model.get('example') 
    if example_value is not None: 
     form.example.data = example_value 

    return render_template('example.html',form=form) 

if __name__ == '__main__': 
    app.run(debug=True) 
+0

感謝は、あなたが助けのためにこれが私の人生そんなに良くなります。私は先に進んでこれを入れてテストします。 – inuasha

関連する問題