本文介绍了flask-wplace占位符行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  class SignUpForm(Form):
username = TextField(Username:,validators =为什么会这样呢?

[/ b] >

  form = SignUpForm()
form.username(placeholder =username)

但是,当您直接使用占位符作为 SignUpForm 的参数时,



$ $ p $ $ $ codelass SignUpForm(Form):
username = TextField(Username:,placeholder =username,validators = [Required ),Length(3,24)])

给出这个错误 TypeError:__init __()得到了一个意想不到的关键字参数'placeholder'



我有点困惑,因为直接定义类和
是一样的 form.username(placeholder =username)但是为什么会发出错误呢?

解决方案

定义一个字段是di不像渲染一个领域。 该库只是没有被设计为在定义字段时采取任意参数。



如果您想要一个快捷方式呈现与标签的字段作为占位符,你可以编写一个Jinja宏。
$ b

  {%macro form_field(field)%} 
{{ field(placeholder = field.label.text)}}
{%endmacro%}


Form:

class SignUpForm(Form):
    username = TextField("Username: ",validators=[Required(),Length(3,24)])

why does this work?

form = SignUpForm()
form.username(placeholder="username")

but not when you directly use placeholder as an argument for the SignUpForm?

class SignUpForm(Form):
        username = TextField("Username: ",placeholder="username",validators=[Required(),Length(3,24)])

it gives out this error TypeError: __init__() got an unexpected keyword argument 'placeholder'

Im a bit puzzled by this because defining it directly on the class should just be the same asdoing form.username(placeholder="username") but why does it give out an error?

解决方案

Defining a field is different than rendering a field. Calling a field to render it accepts arbitrary keyword args to add attributes to the input. The library is just not designed to take arbitrary args when defining the field.

If you want a shortcut to render a field with the label as the placeholder, you can write a Jinja macro.

{% macro form_field(field) %}
{{ field(placeholder=field.label.text) }}
{% endmacro %}

这篇关于flask-wplace占位符行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:46