使用WTForms(Python / Flask)改进简单的自定义URL字段

时间:2015-05-04 09:35:28

标签: python flask widget wtforms flask-wtforms

关于自定义小部件的WTForms documentation非常稀疏,并没有提供我如何实现我想要的提示。我试着玩这些例子,但那不起作用......

有关stackoverflow的其他问题,请不要解释如何开发小部件:

我正在寻找的是一个简单的文本输入字段,它将包含一个URL,旁边有可点击的超链接。所以基本上,自定义字段只会在标准输入字段附加一个超链接,该字段包含其值。

这样的简单,直截了当的例子应该在恕我直言的文档中。

有任何说明或资源说明如何做到这一点?

更新:我的解决方案不够优雅,但有效......

from wtforms import Field
from wtforms.widgets import TextInput

class MyUrlWidget(TextInput):
    def __init__(self):
        super(MyUrlWidget, self).__init__()

    def _value(self):
    if self.data:
        return self.data
    else:
        return ''
    def __call__(self, field, **kwargs):
        w = super(MyUrlWidget, self).__call__(field, **kwargs)
        w = w + '<a href="'+field._value()+'">link</a>'
        return w

class MyUrlInput(Field):
    widget = MyUrlWidget()

任何人都可以帮助改善这一点吗?

1 个答案:

答案 0 :(得分:1)

class UrlWidget(object):
def __call__(self, field, **kwargs):

    href_link = None
    href = None

    # for template kwargs

    if 'example' in kwargs:
        kwargs.setdefault('example', field.example or "example")
    if 'input_type' in kwargs:
        kwargs.setdefault('input_type', field.input_type or "text")
    if 'href_text' in kwargs:
        href_link = kwargs.pop('href_text')
    if 'href' in kwargs:
        href = kwargs.pop('href')

    # for init kwargs

    if field.example:
        kwargs.setdefault('example', field.example)
    if field.input_type:
        kwargs.setdefault('input_type', field.input_type or "text")

    # ext > int if use in template rendering

    html_param = html_params(**kwargs)

    if href or field.href:
        html = "<input %s /><a href=\"%s\" > %s </a>"
        result= HTMLString(html % (html_param, href and href or field.href, href_link and href_link or field.href_text))

    else:
        html = "<input %s />"
        result = HTMLString(html % html_param)

    return result

class UrlField(Field):
widget = UrlWidget()

def __init__(self, href=None, href_text=None, example=None, input_type=None, label=None, validators=None, **kwargs):
    super(UrlField, self).__init__(label=None, validators=None, **kwargs)
    self.example = example
    self.href = href
    self.input_type = input_type
    self.href_text = href_text


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


class SimpleForm(Form):
    newurl = UrlField(href="http://localhost.simple", href_text="Simple link here", example="example", input_type="text")

路由

@app.route('/')
def root():
    form=SimpleForm(request.form)
    return render_template('index.html',form=form)

模板渲染

<body>
Init kwargs: {{ form.newurl }}
Kwargs in template: {{ form.newurl(href="http:/enjoy.dude",  href_text="new link text here for enjoy dude") }}
</body>

html代码

Init kwargs: <input example="example" input_type="text" />
<a href="http://localhost.simple" > Simple link here </a>
Kwargs in template: <input example="example" input_type="text" />
<a href="http:/enjoy.dude" > new link text here for enjoy dude </a>

享受Dude:)

相关问题