WTForms支持输入只读属性? [英] WTForms support for input readonly attribute?

查看:489
本文介绍了WTForms支持输入只读属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处他们表示不支持开箱即用。



您知道一种使HTML输入表单字段与WTForms一起使用'readonly'属性的方法吗?

解决方案

我假设您正在讨论HTML / XHTML中的< input readonly> 属性,这不是您讨论的主题链接是关于。 (链接的线程是关于如何忽略传入的表单输入的较低级别的问题)。

设置只读属性(以及字段上的任何属性)是作为模板中的关键字参数。如果使用Jinja,它看起来像(html5):

  {{form.myfield(readonly = true)}} 

对于XHTML或早于0.6.3的WTForms版本:

  {{form.myfield(readonly =readonly)}} 

请注意,'只读'属性仅仅是浏览器的一个提示,它对用户提交的内容没有影响。这就是说,恶意用户(或者使用带有自定义JS,浏览器或者JS控制台或DOM树的浏览器的用户)可以生成POST请求,以更改字段的值,而不管只读属性是否设置为输入标签。

因此,readonly属性仅用作修改用户体验的选项(例如,基于某些事件/操作禁用字段JS),而来自'只读'字段的输入不会比任何其他表单输入更可靠。


Here they say it's not supported out of the box.

Do you know a way to make HTML input form fields use the 'readonly' attribute with WTForms?

解决方案

I assume you are talking about the <input readonly> attribute in HTML/XHTML, which is not what that discussion thread you linked is about. (the linked thread is about a lower-level issue with how to ignore passed form input)

The way to set a readonly attribute (and indeed any attribute on a field) is as a keyword-arg in your template. If using Jinja, this looks like (html5):

{{ form.myfield(readonly=true) }}

And for XHTML or versions of WTForms older than 0.6.3:

{{ form.myfield(readonly="readonly") }}

Just note that the 'readonly' attribute is only a hint to the browser, and it has no impact on what the user submits. This is to say, a malicious user (or someone using a browser with custom JS a la greasemonkey or a JS console or a DOM tree) could generate a POST request changing the value of a field regardless of whether the readonly attribute is set on the input tag.

For this reason, the readonly attribute is only useful as an option to modify the user experience (for example, disabling a field based on some event/action using JS) and the input coming from a 'readonly' field is no more trust-able than any other form input.

这篇关于WTForms支持输入只读属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆