编程语言
首页 > 编程语言> > python – WTForms创建自定义小部件

python – WTForms创建自定义小部件

作者:互联网

WTForms文档非常不合适,它们甚至没有向您显示自定义窗口小部件的一个示例,该窗口小部件不是从另一个窗口小部件派生的.

我正在尝试创建一个按钮类型,这不是< input>在HTML中:

submit = InlineButton(name='submit', type='submit', title='Save this page', textWithinSpan='Save')

这就是我正在尝试的:

from flask.ext.wtf import Required, Length, EqualTo, Field, TextInput, html_params
from flask import Markup

class InlineButtonWidget(object):
  text = ''
  html_params = staticmethod(html_params)

  def __init__(self, input_type='submit', **kwargs):
    self.input_type = input_type

  def __call__(self, field, **kwargs):
    kwargs.setdefault('id', field.id)
    kwargs.setdefault('type', self.input_type)
    if 'value' not in kwargs:
        kwargs['value'] = field._value()
    return Markup('<button type="submit" %s><span>%s</span></button>' % (self.html_params(name=field.name, **kwargs), kwargs['textWithinSpan']))


class InlineButton(Field):
  widget = InlineButtonWidget()

  def __init__(self, label='', **kwargs):
    self.widget = InlineButtonWidget('submit', label)
  def __call__(self, **kwargs):
    return self.widget(self, **kwargs)
  def _value(self):
    if self.data:
        return u', '.join(self.data)
    else:
        return u''


class SignupForm(Form):
    name = TextField('Name', [Length(min=1, max=200)])
    submit = InlineButton(name='submit', type='submit', title='Save this page', textWithinSpan='Save')

我甚至不需要Field派生对象.但是当您仅使用Widget时它不会显示.

当您使用Field对象时,它会为您提供各种无效的参数错误.

即使深入研究WTForms源代码,也很难理解为什么它不能将Kwargs从表单传递给widget.

—更新—

好的,在我提交问题后,我基本上找到了一个可行的解决方案:

class InlineButtonWidget(object):
    html_params = staticmethod(html_params)

    def __init__(self, input_type='submit', text=''):
        self.input_type = input_type
        self.text = text

    def __call__(self, field, **kwargs):
        kwargs.setdefault('id', field.id)
        kwargs.setdefault('type', self.input_type)
        if 'value' not in kwargs:
            kwargs['value'] = field._value()
        return Markup('<button type="submit" %s><span>%s</span></button>' % (self.html_params(name=field.name, **kwargs), field.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 SignupForm(Form):
    name = TextField('Name', [Length(min=1, max=200)])
    submit = InlineButton('submit', text='Save', description='Save this')

解决方法:

在Update下回答,但在Field派生类中需要这个init.

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

标签:flask-wtforms,wtforms,python,flask
来源: https://codeday.me/bug/20191007/1869563.html