简体   繁体   English

将 WTForms 提交按钮设置为图标

[英]Set WTForms submit button to icon

I want a submit button that displays an icon rather than text.我想要一个显示图标而不是文本的提交按钮。 The button is a field in a WTForms form.该按钮是 WTForms 表单中的一个字段。 I am using Bootstrap and Open Iconic for styling and icons.我使用 Bootstrap 和 Open Iconic 来设计样式和图标。 How do I set the submit field to display an icon?如何设置提交字段以显示图标?

class MyForm(Form):
    submit = SubmitField('')
{{ form.submit }}

This post refers to an icon method, but I can't find any more information on it. 这篇文章提到了一个icon方法,但我找不到更多关于它的信息。

The example you linked is using macros provided by Flask-Bootstrap.您链接的示例使用 Flask-Bootstrap 提供的宏。 The macros make it easy to construct forms with Bootstrap, but might not be flexible.宏使使用 Bootstrap 构建表单变得容易,但可能不灵活。 For example, they probably don't support using Open Iconic icons instead.例如,他们可能不支持使用 Open Iconic 图标。

Typically, you don't need to specify the submit button in the form, as it doesn't contribute useful data.通常,您不需要在表单中指定提交按钮,因为它不会提供有用的数据。 Render the submit as a button in the template manually and set whatever content you need.手动将提交呈现为模板中的按钮并设置您需要的任何内容。

<button type=submit class="btn btn-primary"><span class="oi oi-check" title="Submit"></span></button>

If you do need a SubmitField in your form, you can set the label to a Markup string with the HTML.如果您确实需要SubmitField中的SubmitField ,您可以将标签设置为带有 HTML 的Markup字符串。 The Markup is needed to tell Jinja it's safe to render without escaping.需要Markup来告诉 Jinja 在不转义的情况下渲染是安全的。

from markupsafe import Markup
submit_value = Markup('<span class="oi oi-check" title="Submit"></span>')
submit = SubmitField(submit_value)

I had a similar problem, so, you can do this to use wtforms, and simply call {{ render_temple(form) }} in html to get this result.我遇到了类似的问题,因此,您可以这样做以使用 wtforms,只需在 html 中调用{{ render_temple(form) }}即可获得此结果。

class InlineButtonWidget(object):
    def __init__(self, class_=None):
        self.class_ = class_

    def __call__(self, field, **kwargs):
        kwargs.setdefault('type', 'submit')
        kwargs["class"] = self.class_
        title = kwargs.pop('title', field.description or '')
        params = html_params(title=title, **kwargs)

        html = '<button %s>%s</button>'
        return HTMLString(html % (params, escape(field.label.text)))


class InlineButton(Field):
    widget = InlineButtonWidget()

    def __init__(self, label=None, validators=None, text='Save', **kwargs):
        super(InlineButton, self).__init__(label, validators, **kwargs)
        self.text = text

    def _value(self):
        if self.data:
            return u''.join(self.data)
        else:
            return u''

Usage:用法:

class BookForm(FlaskForm):
    text = Markup('<i class="fas fa-sign-in-alt"></i> Submit')
    submit = SubmitField(text, widget=InlineButtonWidget(class_="btn btn-info")) #This class_ param it's applied in the class of the button in HTML.

Output:输出:

Html code html代码

Display显示

My reply is based on this post WTForms creating a custom widget .我的回复基于这篇文章WTForms created a custom widget

if you want to render a submit field with an image icon instead of submit label text, in your html template, you can do something like this:如果您想使用图像图标而不是提交标签文本呈现提交字段,在您的 html 模板中,您可以执行以下操作:

(assuming your form field name is isubmit): (假设您的表单字段名称是 isubmit):

<button type="submit" style="border: 0 none; background: none;" name="isubmit" value="x"><img src="/static/previousButton.png"></button>

note: value has to be non-empty, but it doesn't matter what it is;注意:值必须是非空的,但它是什么并不重要; only img will be displayed.只会显示 img。

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

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