简体   繁体   English

如何使用 WTForms-Alchemy 添加文本字段占位符

[英]How to add a text-field-placeholder using WTForms-Alchemy

I'm currently using WTForms-Alchemy for creating forms for my database models.我目前正在使用WTForms-Alchemy为我的数据库模型创建表单。

My User.py looks like this:我的 User.py 看起来像这样:

class User(UserMixin, db.Model, CreationDateMixin):
   __tablename__ = 'user'

   id = db.Column(db.Integer, primary_key=True)
   first_name = db.Column(db.String(50), info={
      'label': 'Firstname',
      'validators': validators.Length(min=3)
   })

For the Model I defined a form using WTForms-alchemy:对于模型,我使用 WTForms-alchemy 定义了一个表单:

class EditProfileForm(ModelForm, FlaskForm):
   class Meta:
      model = User

   submit = SubmitField("Update")

And I'm using the form in a Jinja2 Template file like this:我在 Jinja2 模板文件中使用表单,如下所示:

{{ form.short_name }}

Is there a way to define a placeholder for the text input in the User.py class?有没有办法为 User.py 类中的文本输入定义占位符? I already tried adding a the parameter 'placeholder' to the info block but this didn't work.我已经尝试向信息块添加参数“占位符”,但这不起作用。

WTForms has an argument render_kw which is a dict describing arguments to pass to the render widget for the form object. WTForms 有一个参数render_kw ,它是一个描述传递给表单对象的渲染小部件的参数的字典。 [ https://wtforms.readthedocs.io/en/stable/fields.html#the-field-base-class ] [ https://wtforms.readthedocs.io/en/stable/fields.html#the-field-base-class ]

The info argument is a dict of arguments to pass to the WTForms functions so the solution is to pass a dict of another dict: info 参数是传递给 WTForms 函数的参数字典,因此解决方案是传递另一个字典的字典:

class User(UserMixin, db.Model, CreationDateMixin):
   __tablename__ = 'user'

   id = db.Column(db.Integer, primary_key=True)
   first_name = db.Column(db.String(50), info={
      'label': 'Firstname',
      'validators': validators.Length(min=3),
      'render_kw': {'placeholder': 'FIRST NAME'}
   })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM