2016-11-05 58 views
1

我試圖在用戶提交時提出錯誤而未選擇無線電選項,但它不工作。當我渲染這樣的字段:{{ render_field(form.example) }}它打印(self.gettext('PICK SOMETHING'),但是當我使用下面什麼格式發生了:Flask-WTForms如何覆蓋無線電字段上的pre validate

{% for subfield in form.religion %} 
     {{ render_field(subfield, class_="foo") }} 
     {{ render_field(subfield.label) }} 
    {% endfor %} 

radio.py

class ReligionField(SelectField): 
    widget = ListWidget(prefix_label=False) 
    option_widget = RadioInput() 

    def pre_validate(self, form): 
     for v, _ in self.choices: 
      if self.data == v: 
       break 
     else: 
      raise ValueError(self.gettext('PICK SOMETHING')) 

class ReligionForm(FlaskForm): 
    religions = ['Christian', 'Muslim', 'Hindu', 'Buddhist'] 
    choices = [(x.lower(), x.title()) for x in sorted(religions)] 
    religion = ReligionField('Religion', validators=[DataRequired()], choices=choices, render_kw={"class":"post", "type":"radio"}) 

@app.route('/',methods=['post','get']) 
def hello_world(): 
    form = ReligionForm() 
    if form.validate_on_submit(): 
     print form.religion.data 
    else: 
     print form.errors 
    return render_template('radio.html', form=form) 

我如何可以覆蓋pre_validate引發錯誤如果我提交沒有選擇任何選擇?

回答

0

試試這個腳本:

from flask import Flask 
from flask import render_template 
from flask import request 
from flask import flash 
from flask import redirect 
from flask_wtf import FlaskForm 
from wtforms import SelectField 
from wtforms.widgets import ListWidget, RadioInput 
from wtforms.validators import DataRequired 


app = Flask(__name__) 

class ReligionField(SelectField): 
    widget = ListWidget(prefix_label=False) 
    option_widget = RadioInput() 

    def pre_validate(self, form): 
     for v, _ in self.choices: 
      if self.data == v: 
       break 
     else: 
      raise ValueError(self.gettext('PICK SOMETHING')) 

class ReligionForm(FlaskForm): 
    religions = ['Christian', 'Muslim', 'Hindu', 'Buddhist'] 
    choices = [(x.lower(), x.title()) for x in sorted(religions)] 
    religion = ReligionField('Religion', validators=[DataRequired()], choices=choices, render_kw={"class":"post", "type":"radio"}) 


@app.route('/',methods=['GET','POST']) 
def hello_world(): 
    form = ReligionForm(request.form) 

    if request.method == 'POST': 
     if form.validate_on_submit(): 
     print form.religion.data 
     else: 
     print form.religion.errors[0] 
     flash(form.religion.errors[0]) 
     return redirect('/') 
    return render_template('radio.html', form=form) 


if __name__ == "__main__": 
    app.run() 

radio.html

{% with messages = get_flashed_messages() %} 
    {% if messages %} 
    <ul class=flashes> 
    {% for message in messages %} 
     <li>{{ message }}</li> 
    {% endfor %} 
    </ul> 
    {% endif %} 
{% endwith %} 

<form method="post"> 
{% for subfield in form.religion %} 
     <li>{{subfield.label}} {{subfield}} </li> 
    {% endfor %} 

<button>Submit</button> 
</form> 
+0

它不工作。 – roy

+0

你能證明你得到了什麼錯誤嗎?你想要什麼輸出? –

+0

如果我不提交任何內容,我希望它能打印'選擇幾何'。 – roy