如何指定
构造 wtforms 的 TextAreaField 是这样的:
content = wtf.TextAreaField('Content', id="content-area", validators=[validators.Required()])
如何指定与此文本区域关联的行数和列数?
Constructing a wtforms' TextAreaField is something like this:
content = wtf.TextAreaField('Content', id="content-area", validators=[validators.Required()])
How can I specify the number of rows and columns associated with this textarea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您不应该在声明小部件的地方执行此操作。你已经在模板中完成了。例如:
需要确保将行和列指定为字符串。
You are not supposed to do it in the place where you declare the widget. You have do it in the template. For eg:
Need to ensure the rows and cols are specified as a string.
简单多了;创建字段时使用
render_kw
参数:然后渲染文件:
Much simpler; use
render_kw
argument when creating the field:And then render the file:
nettuts+ 有一个关于 Flask 的教程。基本上,它的工作原理如下:
在 html 中:
您可以在 css 中指定布局,而不是在 html 中指定布局,例如:
There is a tutorial on Flask by nettuts+. Basically, it works like this:
and in your html:
Instead of specifying the layout in html, you can specify it in your css, for example:
{{form.text(cols="35", rows="20")|safe}}
正在运行{{form.text(cols="35", rows="20")|safe}}
is working这是一个例子:
Here is an example:
我能够通过 python 表单页面上的 render_kw 标签修改行和列。当我第一次在WTForm的网站上看到这个定义时,我不知道它做了什么,直到看到其他人使用它来修改表单元素的类的示例。所以他们的定义让我很困惑,直到我开始尝试它。
我在我的表单页面上使用了这个。
要添加多个标签和值,只需用逗号分隔它们,如下所示。
然后将其呈现在 HTML 中。请注意添加的“rows”属性。
I was able to modify the rows and columns via the render_kw tag on the python forms page. When I first saw the definition of this on WTForm's website, I didn't know what it did until seeing other people's examples of using this to modify the class of the form element. So their definition just confused me until I started experimenting with it.
I used this on my forms page.
To add multiple tags and values, just separate them with a comma like this.
Then this was rendered in the HTML. Notice the "rows" attribute there that was added.
为了方便起见,先添加这个宏。
_formhelpers.html
:以这种方式导入这个宏和代码。它应该有效。
希望有帮助!
For convenience, add this macro first.
_formhelpers.html
:Import this macro and code in this way. it should work.
Hope it helps !
您可以简单地使用这个记住渲染默认值的替换小部件:
现在您可以将这个新小部件添加到您的字段:
您现在可以在没有任何额外参数的情况下渲染此字段并获得 50x100 文本区域。
You could simply use this replacement widget that is remembering default values for the rendering:
Now you can add this new widget to your Field:
You now can render this field without any extra arguments and get a 50x100 textarea.
我查看了代码,发现 Field 类同时定义了 __new__ 和 __init__ 。
__new__
需要一堆*args
和**kargs
。因此,您只需在 TextAreadField 创建中传递 rows=x cols=y 即可,它应该可以工作。我注意到 wtforms 正在为这种情况创建一个名为“UnboundField”的类,我不知道它的含义,因为这会导致问题(如果有的话)。但创建 TextAreaField 的下一步是相同的。 (即控制权像以前一样转到 __init__ 调用。)I looked at the code and found that Field class defines both
__new__
and__init__
.__new__
takes a bunch of*args
and**kargs
. So, you can just passrows=x cols=y
in your TextAreadField creation and it should work. I noticed that wtforms is creating a class called "UnboundField" for such cases, I don't know the implications of that as where this would cause problems, if any. But the next step of creating the TextAreaField are same. (That is control goes to__init__
call as before.)