WTForms-如何预填充文本区域字段?

发布于 2024-10-19 03:11:43 字数 216 浏览 1 评论 0原文

嗨,我一直在尝试使用某些东西来填充文本区域字段 就像模板中这样。

{{form.content(value="please type content")}}

当字段是文本字段时,这有效,主要是因为 html 接受 的值 但同样不适用于文本区域...... 有人可以帮我解决这个问题吗?

Hi I have been trying to pepopulate a textareafield using something
like this in the template.

{{form.content(value="please type content")}}

This works when the field is textfield primarily because the html
accepts value for <input type="text">
but the same does not work for textarea...
Can someone please help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

囚你心 2024-10-26 03:11:43

您可以在渲染之前执行此操作,例如:

form.content.data = 'please type content'

不过,我是 WTForms 的新手。

You can do it before rendering, something like:

form.content.data = 'please type content'

I'm new to WTForms though.

万劫不复 2024-10-26 03:11:43

对于 textarea 小部件,您可以在字段构造函数中使用 default 参数设置默认内容。

class YourForm(Form):
    your_text_area = TextAreaField("TextArea", default="please add content")

然后,当您渲染时:

{{form.content()}}

WTForms 将渲染默认文本。我无法找到一种方法来在渲染时指定文本区域的默认文本。

For textarea widgets, you set the default content with the default argument in your field constructors.

class YourForm(Form):
    your_text_area = TextAreaField("TextArea", default="please add content")

Then when you render:

{{form.content()}}

WTForms will render the default text. I have not been able to find a way to specify default text for the text area at render time.

狼亦尘 2024-10-26 03:11:43

我最近遇到了同样的问题,我是这样解决的:

{% set f = form.content.process_data("please type content") %}
{{ form.content() }}

对于测试,您可以尝试运行以下代码片段:

>>> import wtforms
>>> import jinja2
>>> from wtforms.fields import TextAreaField
>>> class MyForm(wtforms.Form):
...     content = TextAreaField("Text Area")
... 
>>> t = jinja2.Template("""{% set f = form.content.process_data("please type content") %}
...                     {{ form.content() }}""")
>>> 
>>> t.render(form=MyForm())
u'\n                    <textarea id="content" name="content">please type content</textarea>'

I recently had the same problem, I solved it like this:

{% set f = form.content.process_data("please type content") %}
{{ form.content() }}

For a test, you can try run the follow snippet:

>>> import wtforms
>>> import jinja2
>>> from wtforms.fields import TextAreaField
>>> class MyForm(wtforms.Form):
...     content = TextAreaField("Text Area")
... 
>>> t = jinja2.Template("""{% set f = form.content.process_data("please type content") %}
...                     {{ form.content() }}""")
>>> 
>>> t.render(form=MyForm())
u'\n                    <textarea id="content" name="content">please type content</textarea>'
静赏你的温柔 2024-10-26 03:11:43

对于那些尝试在 jinja 模板中动态执行此操作的人来说,在渲染之前设置默认值并不能解决此问题。

不使用 WTForms 标准:

{{ form.content() }}

您可以使用原始 HTML 构造此元素,例如:

<textarea id="FormName-content" name="FormName-content">{{ dynamic values here }}</textarea>

...并且它仍然可以与您的 WTForms 验证一起使用。

希望这对某人有帮助:)

For those trying to do this dynamically in jinja template, setting the default value prior to rendering does not fix this problem.

Instead of using the WTForms standard:

{{ form.content() }}

You can construct this element with raw HTML like:

<textarea id="FormName-content" name="FormName-content">{{ dynamic values here }}</textarea>

...and it will still work with your WTForms validation.

Hope this helps someone :)

江南月 2024-10-26 03:11:43

爱丽丝,表单小部件中似乎内置了支持来完成您要做的事情,但您是对的,它目前不起作用。

肖恩和希尔奎斯发布了确实有效的体面工作环境。这是你可以尝试的形式(yuk yuk)

 else:
        form.title.data=blog.title
        form.blogdata.data=blog.blogdata
    return render_template('editblog.html',form=form)

Alice there seems to be support built into the form widget to do what you are after but you are right it doesn't work at the moment.

Sean and hilquias post decent work arounds which do work. This is the form (yuk yuk) you might try

 else:
        form.title.data=blog.title
        form.blogdata.data=blog.blogdata
    return render_template('editblog.html',form=form)
ゞ花落谁相伴 2024-10-26 03:11:43

使用 TextArea 字段定义表单

class MyForm(Form):
    name = fields.StringField('Name', validators=[Required()])
    description = fields.TextAreaField('Description')

在渲染模板之前设置文本区域内容

form = MyForm()
form.description.data='this is my textarea content!' # This is the stuff...
return render_template('form.html', form=form, name=name)

在模板中渲染字段

<form ...>
    {{ field(form.name, value=name) }}
    {{ field(form.description, rows=2) }}
    <button ...>Save</button>
</form>

Define your form with the TextArea field

class MyForm(Form):
    name = fields.StringField('Name', validators=[Required()])
    description = fields.TextAreaField('Description')

Set the textarea content before rending the template

form = MyForm()
form.description.data='this is my textarea content!' # This is the stuff...
return render_template('form.html', form=form, name=name)

Render the fields in your template

<form ...>
    {{ field(form.name, value=name) }}
    {{ field(form.description, rows=2) }}
    <button ...>Save</button>
</form>
っ〆星空下的拥抱 2024-10-26 03:11:43

该线程有点旧,但如果您需要使用动态内容预填充 textarea 字段,您可以使用 setattr 和 default 参数,如下所示:

if post.content:

    form = EditPostForm
    setattr(form, "content", TextAreaField(gettext('Post content'), validators=[Length(max=5000)], default=post.content))
    form = EditPostForm()

else:
    form = EditPostForm()

不要忘记导入 TextAreaField。

This thread is a bit old but if you need to prepopulate the textarea field with dynamic content, you could use setattr and the default parameter like so:

if post.content:

    form = EditPostForm
    setattr(form, "content", TextAreaField(gettext('Post content'), validators=[Length(max=5000)], default=post.content))
    form = EditPostForm()

else:
    form = EditPostForm()

Don't forget to import TextAreaField.

笑叹一世浮沉 2024-10-26 03:11:43

您还可以通过将文本区域字段传递到 render_template 中的 WTforms 类实例化来预填充文本区域字段(在本示例中,我使用 Flask 框架):

@admin.route('/edit/<id>', methods=['GET', 'POST'])
def edit(id):
  if request.method == 'POST':
    form = ProspectForm(request.form)
    prospect = Prospect.objects(id=id).first()
    return render_template('admin/edit.html', prospect=prospect, prospect_form=ProspectForm(comments = prospect.comments))

因此,comments=prospect.comments 表示“在 TextAreaField 名为 'comments' 的字段到前景.comments 中包含的数据"

You can also pre-populate a textarea field by passing it in to your WTforms class instantiation in render_template (in this example I'm using the Flask framework):

@admin.route('/edit/<id>', methods=['GET', 'POST'])
def edit(id):
  if request.method == 'POST':
    form = ProspectForm(request.form)
    prospect = Prospect.objects(id=id).first()
    return render_template('admin/edit.html', prospect=prospect, prospect_form=ProspectForm(comments = prospect.comments))

So, comments=prospect.comments says "set the text inside the TextAreaField field named 'comments' to the data contained in prospect.comments"

谁把谁当真 2024-10-26 03:11:43

在 Textarea 中,如果使用 jquery,则需要使用 innerHTML 或 html()。

在您的上下文中应该是:

form.content.innerHTML = "please type content";

//using jquery
$('#element').html("please type content");

希望有帮助。

In a Textarea you will need to use innerHTML or html() if you use jquery.

in your context should be:

form.content.innerHTML = "please type content";

//using jquery
$('#element').html("please type content");

Hope it helps.

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