简体   繁体   English

模板上的SelectField WTForm呈现

[英]SelectField WTForm rendering on Template

I've been struggling for nearly a day on a simple rendering of a form field. 在表单字段的简单呈现方面,我已经花了将近一天的时间。 That would be great if you could help me on this one. 如果您能在这一方面帮助我,那将是很棒的。

I'm using Flask-WTF, python 2.7. 我正在使用Flask-WTF,python 2.7。

I'm trying to render a SelectField using a custom ListWidget. 我正在尝试使用自定义ListWidget渲染SelectField。 The field basically must be rendered within a UL HTML tag, rather than a SELECT html tag, and this seems to be what I'm struggling with. 基本上,该字段必须呈现在UL HTML标记内,而不是SELECT html标记内,这似乎是我一直在努力的目标。

This is how my custom widget class is: 这是我的自定义小部件类的方式:

widget.py widget.py

class CustomListWidget(ListWidget):

    def __init__(self, *args, **kwargs):
        super(CustomListWidget, self).__init__(*args, **kwargs)

    def __call__(self, field, **kwargs):
        kwargs.setdefault('id', field.id)
        html = ['<{} {}>'.format(self.html_tag, html_params(**kwargs))]
        for subfield in field:
            html.append('<li><span>{}</span></li>'.format(subfield()))
        html.append('</{}>'.format(self.html_tag))
        return HTMLString(''.join(html))

This is how my form looks, and the category field is the one I'm struggling with. 这是我的表格的外观,类别字段是我一直在努力的领域。

form.py form.py

from widget import CustomListWidget
from wtforms import SelectField, SubmitField


widget = CustomListWidget(html_tag='ul')

class MyForm(Form):
    category = SelectField('category', [DataRequired()], widget=widget, default='1', choices=[
        ('1', 'whatever'),
        ('2', 'whatever2')
    ])
    submit = SubmitField('Search')

view.py view.py

from form import MyForm
from flask import render_template

@app.route('/formtest', methods=['GET', 'POST'])
def formtest():
    form = MyForm()
    if request.method == 'GET':
        render_template('form.html', form=form)

    if form.validate_on_submit():
        return redirect(url_for('whatever', data=-form.data))
    return 'form not validated'

form.html form.html

<div class="search-input with-dropdown">
    <div class="dropdown">
       {{ form.category(class='dropdown-content hide') }} }}
    </div>
</div>

With this code I'm able to get the display expected, but no value are being passed from the field. 使用此代码,我可以得到预期的显示,但是没有从该字段传递任何值。 Whichever value I select, after I submit, only the default value is there. 提交后,无论选择哪个值,都只有默认值。

I've done something similar recently with a loop to iterate over all the choices. 最近,我做了一个循环遍历所有选择的类似操作。 Basically, I've created a SelectMultipleField copy to have my own html for it as we needed specific stuff. 基本上,我已经创建了一个SelectMultipleField副本,以便为它添加自己的html,因为我们需要特定的东西。

As you can see in the snipped in bellow, I iterate over the field.iter_choices() . 如您所见,在下面的field.iter_choices() ,我遍历field.iter_choices()

{%- if field.type in ['SelectMultipleField'] %}
    <ul>
    {% for key, value, checked in field.iter_choices() %}
        <li>{{ value }}</li>
    {% endfor %}
    </ul>
{%- endif %}

You can read more about custom widgets in http://wtforms.simplecodes.com/docs/0.6/widgets.html 您可以在http://wtforms.simplecodes.com/docs/0.6/widgets.html中了解有关自定义窗口小部件的更多信息

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

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