Here they say it's not supported out of the box.WTForms支持输入只读属性?
你知道一种方法,使HTML输入表单字段使用“只读”与WTForms属性?
Here they say it's not supported out of the box.WTForms支持输入只读属性?
你知道一种方法,使HTML输入表单字段使用“只读”与WTForms属性?
我假设你在谈论HTML/XHTML中的<input readonly>
属性,这不是你所链接的讨论线索的意思。 (链接线程是关于如何忽略传入的表单输入的较低级别问题)
设置只读属性(实际上是字段上的任何属性)的方法是在模板中作为关键字参数。如果使用神社,这看起来像(HTML5):
{{ form.myfield(readonly=true) }}
而对于XHTML或WTForms版本低于0.6.3以上:
{{ form.myfield(readonly="readonly") }}
只是注意,“只读”属性只是一个提示到浏览器,并且它对用户提交的内容没有影响。这就是说,恶意用户(或者使用带有自定义JS,浏览器或者JS控制台或DOM树的浏览器的用户)可以生成POST请求,以更改字段的值,而不管只读属性是否设置为输入标签。
因此,readonly属性仅作为修改用户体验的选项(例如,基于某些使用JS的事件/操作禁用字段),而来自'readonly'字段的输入为no比任何其他形式的输入更可信。
https://wtforms-components.readthedocs.org/en/latest/#
from wtforms import Form, DateField, TextField
from wtforms_components import TimeField, read_only
class EventForm(Form):
name = TextField('Name')
start_date = DateField('Start date')
start_time = TimeField('Start time')
def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args, **kwargs)
read_only(self.name)
将溶液在表单字段声明使用render_kw
。
my_field = fields.StringField('Label', render_kw={'readonly': True})
而且,如果你想从Jinja2访问它,你可以这样做: {%if form.my_field.render_kw.readonly} readonly blabla {%endif%} – techniao 2017-08-25 22:19:10
这似乎有点傻,你不能在通过这些作为kwargs到Widget或当你宣布你的形式的东西。 – coleifer 2014-02-22 03:02:05