我几乎得到了我的验证消息本地化,因为你可以看到它适用于英语和瑞典语:
英语:

瑞典:

但是当我切换到葡萄牙语时,我收到以下错误消息:
Traceback (most recent call last):
File "/media/Lexar/montao/lib/webapp2/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/media/Lexar/montao/montaoproject/main.py", line 1749, in post
current_user=self.current_user,
File "/media/Lexar/montao/montaoproject/main.py", line 466, in render_jinja
self.response.out.write(template.render(data))
File "/media/Lexar/montao/montaoproject/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "/media/Lexar/montao/montaoproject/templates/insert_jinja.html", line 249, in top-level template code
<ul class="errors">{% for error in form.name.errors %}<li>{{ error }}</li>{% endfor %}</ul>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我想我以前有这个错误信息,我不知道如何处理它.请你帮助我好吗?为什么会出现此错误消息?
我的表单类的代码是
class AdForm(Form):
my_choices = …Run Code Online (Sandbox Code Playgroud) 我已经使用 wtforms 创建了一个注册表单。我在其中使用 FormField 以便我不必再次重复表单的某些元素。但是每当我点击提交按钮时,它总是在 validate_on_submit 方法调用时给我错误。不明白为什么会这样。
我form.py的如下:
class ProfileInfoForm(Form):
firstname = TextField('firstname', validators=
[validators.Required("Please enter First name.")])
lastname = TextField('lastname', validators=
[validators.Required("Please enter Last name.")])
email = EmailField('email', validators=
[validators.Required("Please enter your valid email.")])
gender = RadioField('gender', validators=
[validators.Required("Please select gender")],
choices=[('female', 'Female'), ('male', 'Male')])
dob = TextField('dob', validators=
[validators.Required("Please select date of birth.")])
languages = SelectMultipleField('languages', choices=[('', '')],
validators=
[validators.Required("Please select\
atleast one \
language.")])
class RegistrationForm(Form):
profilefield = FormField(ProfileInfoForm)
password = PasswordField('password',
validators=
[validators.Required("Please enter …Run Code Online (Sandbox Code Playgroud) 在我的表单上,我有两个按钮用于提交表单。一个按钮删除选定的文件(显示在表格中,一个复选框用于对象),另一个按钮选择它们进行处理。
当文件在删除时被选择时,不需要验证(除了检查是否至少选择了一个文件)。但是,为了进行处理,我需要确保只有一个具有特定扩展名的文件。基本上,我需要根据用户单击的按钮进行不同的验证过程。
我怎样才能最好地做到这一点?我知道我可以在视图中执行验证,但我更喜欢在表单内验证它,因为它更干净。
这是有问题的表格:
class ButtonWidget(object):
"""A widget to conveniently display buttons.
"""
def __call__(self, field, **kwargs):
if field.name is not None:
kwargs.setdefault('name', field.name)
if field.value is not None:
kwargs.setdefault('value', field.value)
kwargs.setdefault('type', "submit")
return HTMLString('<button %s>%s</button>' % (
html_params(**kwargs),
escape(field._value())
))
class ButtonField(Field):
"""A field to conveniently use buttons in flask forms.
"""
widget = ButtonWidget()
def __init__(self, text=None, name=None, value=None, **kwargs):
super(ButtonField, self).__init__(**kwargs)
self.text = text
self.value = value
if name is not None:
self.name = name
def …Run Code Online (Sandbox Code Playgroud) 我想使用Jquery添加或删除带有按钮的新WTForm输入字段,就像这里 http://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery/comment-page-1 但是使用我的表格字段.
我的表格:
class EditBook(Form):
title = TextField('title', validators = [Required()])
authors = FieldList(TextField())
Run Code Online (Sandbox Code Playgroud)
问题是我不能只是附加例子
$(InputsWrapper).append("{{form.authors(size=20)}}");
Run Code Online (Sandbox Code Playgroud)
它只是打印这个文本.
我有一个Prediction模型列表.我想将它们绑定到表单并允许使用回发.我如何构建我的表单,以便帖子将Home/Away得分与我绑定到表单的每个项目的Prediction模型id字段相关联?
视图
@app.route('/predictor/',methods=['GET','POST'])
@login_required
def predictions():
user_id = g.user.id
prediction= # retrieve prediction
if request.method == 'POST':
if form.validate() == False:
flash('A score is missing, please fill in all predictions')
render_template('predictor.html', prediction=prediction, form=form)
else:
for pred in prediction:
# store my prediction
flash('Prediction added')
return redirect(url_for("predictions"))
# display current predictions
elif request.method == 'GET':
return render_template('predictor.html', prediction=prediction, form=form)
Run Code Online (Sandbox Code Playgroud)
形成
class PredictionForm(WTForm):
id = fields.IntegerField(validators=[validators.required()], widget=HiddenInput())
home_score = fields.TextField(validators=[validators.required()])
away_score = fields.TextField(validators=[validators.required()])
Run Code Online (Sandbox Code Playgroud)
模板
<form action="" …Run Code Online (Sandbox Code Playgroud) 我正在使用无线电场,我希望将默认值渲染为(.)而不是().我尝试了直截了当的方法:
choice_switcher = RadioField('Choice?', [validators.Required()], choices=[('choice1', 'Choice One'),('choice2', 'Choice Two')], default='choice1')
Run Code Online (Sandbox Code Playgroud)
它没用.它提供两种选择:
( ) Choice One
( ) Choice Two
Run Code Online (Sandbox Code Playgroud)
虽然我想看到这个:
(.) Choice one
( ) Choice Two
Run Code Online (Sandbox Code Playgroud) 这是我的WTForm
from flask.ext.wtf import Form
from wtforms import TextField, PasswordField, SelectMultipleField,HiddenField,validators
from wtforms.validators import Required, Email, EqualTo
class CreateCar(Form):
id = HiddenField ('id')
plaque_no = TextField('plaque_no', [Required(message='pn')])
plaque_img = TextField('plaque_img', [Required(message='pi')])
Run Code Online (Sandbox Code Playgroud)
这是我的Flask视图:
def manage(task=None,id=None):
form = CreateCar
return render('MyHTML.html', form=form)
Run Code Online (Sandbox Code Playgroud)
每当我试图通过这个来覆盖我的表单字段时:
{% for item in form %}
{{ item }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
TypeError: 'FormMeta' object is not iterable
Run Code Online (Sandbox Code Playgroud)
我的表格有什么问题?谢谢
我尝试将flask-wtf与下面的bootstrap-select结合起来,但我修改了TemplateSyntaxError:函数调用表达式的语法无效
<form class="form-inline" action="/" method="POST" role="form">
<div class="input-group">
{{ form.hidden_tag() }}
<span class="input-group-addon">Make:</span>
{{ form.make(id="make_select", class="selectpicker form-control", data-live-search="true") }}
<span class="input-group-addon">Model:</span>
{{ form.model(id="model_select", class="selectpicker form-control", data-live-search="true") }}
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Search!</button>
</span>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
。
TemplateSyntaxError: invalid syntax for function call expression
Traceback (most recent call last):
File "/home/mic/.virtualenvs/unisnp/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/mic/.virtualenvs/unisnp/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/mic/.virtualenvs/unisnp/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, …Run Code Online (Sandbox Code Playgroud) flask wtforms flask-wtforms twitter-bootstrap-3 bootstrap-select
我想要做的是在5个不同的字段上获取用户输入,然后让用户输入的内容可用于我在python中的其余程序中.我到目前为止的代码如下所示:
class ArtistsForm(Form):
artist1 = StringField('Artist 1', validators=[DataRequired()])
artist2 = StringField('Artist 2', validators=[DataRequired()])
artist3 = StringField('Artist 3', validators=[DataRequired()])
artist4 = StringField('Artist 4', validators=[DataRequired()])
artist5 = StringField('Artist 5', validators=[DataRequired()])
@app.route('/', methods=('GET', 'POST'))
def home():
form = ArtistsForm(csrf_enabled=False)
if form.validate_on_submit():
return redirect('/results')
return render_template('home.html', form=form)
@app.route('/results', methods=('GET', 'POST'))
def results():
artist1 = request.form['artist1']
artist2 = request.form['artist2']
artist3 = request.form['artist3']
artist4 = request.form['artist4']
artist5 = request.form['artist5']
return render_template('results.html')
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?(我知道csrf_enabled = False很糟糕,我现在只是为了测试它.)我想要一个低于此功能的值,做一些计算(没有web方面).
编辑:我更新了上面的代码.我能够获得值.现在我只需将它们传递给下面的函数.
我有一个烧瓶应用程序,用户可以在其中提交房间。在我的WTForms中有一个价格字段,它是FloatField:
preis = FloatField('Preis p.P.', validators=[Optional()])
Run Code Online (Sandbox Code Playgroud)
如果输入正确(带点),则可以正常工作,例如:
1.00
Run Code Online (Sandbox Code Playgroud)
但是,如果使用逗号,则会触发错误,例如:
1,00
Run Code Online (Sandbox Code Playgroud)
我的想法是在main.py中捕获此错误,但问题是WTForms的默认错误消息首先触发:
我试图将浮点数转换为字符串,检查是否,在此字符串中,并使用一个简单的值replace(",","."),然后再转换回浮点数。
另一个问题是,如何将此默认Not a valid float value消息更改为自定义消息?
谢谢!