简体   繁体   English

使用 Flask-WTForms,如何设置 html 表单部分的样式?

[英]Using Flask-WTForms, how do I style my form section of the html?

I read through Flask-WTF extremely simplified wiki, and couldn't understand much about what I can do with it.我通读了 Flask-WTF 极其简化的 wiki,但不太了解我可以用它做什么。 I am under the impression that the <form> section of the html now can only look like我的印象是 html 的<form>部分现在只能看起来像

<form method="post">
    {{ form.hidden_tag() }}
    {{ form.name }}
    <input type="submit">
</form>

But I really want to style my using materialized such as:但我真的很想设计我使用物化的样式,例如:

        <div class="row">
            <div class="input-field col s6">
                <i class="material-icons prefix">account_circle</i>
                <input value="FN" id="first_name" type="text" class="validate">
                <label class="active" for="first_name">First Name</label>
            </div>
            <div class="input-field col s6">
                <input value="LN" id="last_name" type="text" class="validate">
                <label class="active" for="last_name">Last Name</label>
            </div>
        </div>

Where can I fit {{ form.first_name }} and {{ form.last_name }} into?我在哪里可以放入{{ form.first_name }}{{ form.last_name }}

EDIT: Let me elaborate on my answer a bit more: For example, I want something like Materialized datepicker (a good pop up calendar that lets user to choose the date), this should be in the <input class='datepicker' length="50"> , but now that I am replacing the whole <input> line with {{ form.date }} ... I lost that privilege to edit the class and what not.编辑:让我详细说明一下我的回答:例如,我想要像 Materialized datepicker这样的东西(一个很好的弹出式日历,让用户可以选择日期),这应该在<input class='datepicker' length="50"> ,但现在我用{{ form.date }}替换了整个<input>行......我失去了编辑课程的特权等等。

WTForms fields can be called with attributes that will be set on the input they render.可以使用将在它们呈现的输入上设置的属性来调用WTForms 字段。 Pass the attributes you need for styling, JavaScript functionality, etc. to the fields, rather than just referencing the fields.将样式、JavaScript 功能等所需的属性传递给字段,而不仅仅是引用字段。 The labels behave the same way, and can be accessed with field.label .标签的行为方式相同,可以使用field.label访问。

for , value , type , id , and name do not need to be passed, as they are handled automatically. forvaluetypeidname不需要传递,因为它们是自动处理的。 There are some rules for handling special cases of attributes.处理属性的特殊情况有一些规则 If an attribute name is a Python keyword such as class , append an underscore: class_ .如果属性名称是 Python 关键字,例如class ,请附加下划线: class_ Dashes are not valid Python names, so underscores between words are converted to dashes;破折号不是有效的 Python 名称,因此单词之间的下划线将转换为破折号; data_toggle becomes data-toggle . data_toggle变为data-toggle

{{ form.first_name(class_='validate') }}
{{ form.first_name.label(class_='active') }}

{{ form.begins(class_='datepicker', length=50) }}

Note that neither of the linked methods need to be called directly, those docs just describe the behavior when calling the fields.请注意,这两个链接方法都不需要直接调用,这些文档只是描述了调用字段时的行为。

In WTForms 2.1 I use extra_classes , like the line below:WTForms 2.1中,我使用extra_classes ,如下所示:

1. The first way 1.第一种方式

{{ f.render_form_field(form.first_name, extra_classes='ourClasses') }}

or maybe in your case just like this:或者在你的情况下可能是这样的:

{{ form.first_name, extra_classes='ourClasses' }}

We can also use the render_kw attribute on our form field like the second way below:我们还可以像下面的第二种方式一样在我们的表单字段上使用render_kw属性:

2. The second way 2.第二种方式

style={'class': 'ourClasses', 'style': 'width:50%; other_css_style;'}
first_name = StringField('First name',
                     validators=[InputRequired(),Length(1, 64)],
                     render_kw=style)

But I would prefer to use the first way.但我更愿意使用第一种方式。

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

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