如何指定

发布于 2024-10-16 22:26:16 字数 187 浏览 2 评论 0原文

构造 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

熊抱啵儿 2024-10-23 22:26:16

您不应该在声明小部件的地方执行此操作。你已经在模板中完成了。例如:

{{form.content(rows='50',cols='100')}}

需要确保将行和列指定为字符串。

You are not supposed to do it in the place where you declare the widget. You have do it in the template. For eg:

{{form.content(rows='50',cols='100')}}

Need to ensure the rows and cols are specified as a string.

神妖 2024-10-23 22:26:16

简单多了;创建字段时使用 render_kw 参数:

port = IntegerField(u"HTTP port", validators=[DataRequired(), NumberRange(1025, 65535)], render_kw={'class': 'form-control'})
mytextarea = TextAreaField(u"Content", render_kw={'class': 'form-control', 'rows': 5})

然后渲染文件:

{{ field() }}

Much simpler; use render_kw argument when creating the field:

port = IntegerField(u"HTTP port", validators=[DataRequired(), NumberRange(1025, 65535)], render_kw={'class': 'form-control'})
mytextarea = TextAreaField(u"Content", render_kw={'class': 'form-control', 'rows': 5})

And then render the file:

{{ field() }}
拥抱我好吗 2024-10-23 22:26:16

nettuts+ 有一个关于 Flask 的教程。基本上,它的工作原理如下:

from flask.ext import wtf

class ContactForm(wtf.Form):
    content = wtf.TextAreaField("Content", [validators.Required()])

在 html 中:

<form action="" method="post">
    {{ form.content }}
</form>

您可以在 css 中指定布局,而不是在 html 中指定布局,例如:

form textarea#content {
     width: 100px;
     height: 100px;
     max-width: 100px;
}

There is a tutorial on Flask by nettuts+. Basically, it works like this:

from flask.ext import wtf

class ContactForm(wtf.Form):
    content = wtf.TextAreaField("Content", [validators.Required()])

and in your html:

<form action="" method="post">
    {{ form.content }}
</form>

Instead of specifying the layout in html, you can specify it in your css, for example:

form textarea#content {
     width: 100px;
     height: 100px;
     max-width: 100px;
}
戏舞 2024-10-23 22:26:16

{{form.text(cols="35", rows="20")|safe}} 正在运行

{{form.text(cols="35", rows="20")|safe}} is working

江挽川 2024-10-23 22:26:16

这是一个例子:

<div class="row">
    <div class="col-md-12">
        <div class="form-group">
            {{ wtf.form_field(form.notes, rows=5) }}
        </div>
    </div>
</div>

Here is an example:

<div class="row">
    <div class="col-md-12">
        <div class="form-group">
            {{ wtf.form_field(form.notes, rows=5) }}
        </div>
    </div>
</div>
兰花执着 2024-10-23 22:26:16

我能够通过 python 表单页面上的 render_kw 标签修改行和列。当我第一次在WTForm的网站上看到这个定义时,我不知道它做了什么,直到看到其他人使用它来修改表单元素的类的示例。所以他们的定义让我很困惑,直到我开始尝试它。

render_kw (dict) – 如果提供的话,字典提供默认关键字,这些关键字将在渲染时提供给小部件。
- https://wtforms.readthedocs.io/en/stable/fields.html


我在我的表单页面上使用了这个。

current_templates_in_library = TextAreaField('current_templates_in_library', render_kw={'rows':'4'})

要添加多个标签和值,只需用逗号分隔它们,如下所示。

render_kw={'class':'myclass','rows':'4'}

然后将其呈现在 HTML 中。请注意添加的“rows”属性。

<textarea class="form-control" id="current_templates_in_library" name="current_templates_in_library" rows="4" style="z-index: auto; position: relative; line-height: 20px; font-size: 14px; transition: none 0s ease 0s; background: transparent !important;">
rhel6
rhel7
win_2012r2
centos7
</textarea>

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.

render_kw (dict) – If provided, a dictionary which provides default keywords that will be given to the widget at render time.
- https://wtforms.readthedocs.io/en/stable/fields.html

I used this on my forms page.

current_templates_in_library = TextAreaField('current_templates_in_library', render_kw={'rows':'4'})

To add multiple tags and values, just separate them with a comma like this.

render_kw={'class':'myclass','rows':'4'}

Then this was rendered in the HTML. Notice the "rows" attribute there that was added.

<textarea class="form-control" id="current_templates_in_library" name="current_templates_in_library" rows="4" style="z-index: auto; position: relative; line-height: 20px; font-size: 14px; transition: none 0s ease 0s; background: transparent !important;">
rhel6
rhel7
win_2012r2
centos7
</textarea>
一页 2024-10-23 22:26:16

为了方便起见,先添加这个宏。

_formhelpers.html

{% macro render_field(field) %}
  <dt>{{ field.label }}
  <dd>{{ field(**kwargs)|safe }}
  {% if field.errors %}
    <ul class=errors>
    {% for error in field.errors %}
      <li>{{ error }}</li>
    {% endfor %}
    </ul>
  {% endif %}
  </dd>
{% endmacro %}

以这种方式导入这个宏和代码。它应该有效。

{% from "_formhelpers.html" import render_field %}
<form action="" method="post">
    {{ render_field(form.content,rows=100, cols=100) }}
</form>

希望有帮助!

For convenience, add this macro first.

_formhelpers.html:

{% macro render_field(field) %}
  <dt>{{ field.label }}
  <dd>{{ field(**kwargs)|safe }}
  {% if field.errors %}
    <ul class=errors>
    {% for error in field.errors %}
      <li>{{ error }}</li>
    {% endfor %}
    </ul>
  {% endif %}
  </dd>
{% endmacro %}

Import this macro and code in this way. it should work.

{% from "_formhelpers.html" import render_field %}
<form action="" method="post">
    {{ render_field(form.content,rows=100, cols=100) }}
</form>

Hope it helps !

完美的未来在梦里 2024-10-23 22:26:16

您可以简单地使用这个记住渲染默认值的替换小部件:

import wtforms.widgets.core

class TextArea(wtforms.widgets.core.TextArea):
    def __init__(self, **kwargs):
        self.kwargs = kwargs

    def __call__(self, field, **kwargs):
        for arg in self.kwargs:
            if arg not in kwargs:
                kwargs[arg] = self.kwargs[arg]
        return super(TextArea, self).__call__(field, **kwargs)

现在您可以将这个新小部件添加到您的字段:

content = wtf.TextAreaField(
    'Content',
    id='content-area',
    widget=TextArea(rows=50,cols=100),
    validators=[validators.Required()])

您现在可以在没有任何额外参数的情况下渲染此字段并获得 50x100 文本区域。

You could simply use this replacement widget that is remembering default values for the rendering:

import wtforms.widgets.core

class TextArea(wtforms.widgets.core.TextArea):
    def __init__(self, **kwargs):
        self.kwargs = kwargs

    def __call__(self, field, **kwargs):
        for arg in self.kwargs:
            if arg not in kwargs:
                kwargs[arg] = self.kwargs[arg]
        return super(TextArea, self).__call__(field, **kwargs)

Now you can add this new widget to your Field:

content = wtf.TextAreaField(
    'Content',
    id='content-area',
    widget=TextArea(rows=50,cols=100),
    validators=[validators.Required()])

You now can render this field without any extra arguments and get a 50x100 textarea.

多情癖 2024-10-23 22:26:16

我查看了代码,发现 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 pass rows=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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文