我有一个类似的问题,所以,你可以这样做来使用 wtforms,并简单地调用{{ render_temple(form) }}
html 来得到这个结果。
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''
用法:
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.
输出:
html代码
展示
我的回复是基于这篇文章WTForms created a custom widget。