指定css类以形成从flask传递给jinja的元素

时间:2017-08-08 13:25:41

标签: python css flask jinja2 flask-wtforms

我对jinja / flask很新,我在我的烧瓶应用程序中创建了dymnamic形式:

class CreateForm(FlaskForm):
    searchCity = StringField('View forcast of city:', validators=[InputRequired("Please enter the city you want to check weather updates")])
    count = IntegerField("Days")
    submit = SubmitField("Submit")


form = CreateForm(request.form)
form.count.default = count
form.count.label = "Days" if count > 1 else "Day" 

form.count.data = count

并在jinja模板中:

<form>
    <dl>
        <dd>{{ form.searchCity.label }} {{ form.searchCity(size=20) }} 
            for next {{ form.count(size=2) }} {{ form.count.label }}
            <input type="submit" class="btn-primary" value="Submit" id="calculate" onclick="getWeatherForcaset(
            document.getElementsByName('searchCity')[whole_number].value,
            document.getElementsByName('count')[whole_number].value
            );" >
        </dd>
        <dd>
            <input type="checkbox" name="exactMatch" checked="checked">Exact Match 
            &nbsp;
            <input type="checkbox" name="remember">Remember<br/ >
        </dd>

    </dl>
 </form>

我想将课程form-group分配给searchCity输入框! 任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

您应该能够将变量传递给构造函数,如此

{{ form.searchCity(size=20, class_='searchCity') }}  

文档位于:

http://wtforms.readthedocs.io/en/latest/crash_course.html#rendering-fields

Doc Snippet

  

然而,真正的力量来自于用它渲染场   调用()方法。通过调用该字段,您可以提供关键字参数,这些参数将作为输出中的html属性注入:

     

form.content(style =“width:200px;”,class _ =“bar”)

<input class="bar" id="content" name="content" style="width: 200px;" type="text" value="foobar" />'

答案 1 :(得分:0)

WTForms 2.1 中,我使用extra_classes,就像下面这样:

1。第一种方式

{{ f.render_form_field(form.searchCity, extra_classes='ourClasses') }}

或者您可能就是这样:

{{ form.searchCity, extra_classes='form-group' }}

我们还可以像下面的第二种方式一样在表单字段上使用render_kw属性:

2。第二种方式

style={'class': 'form-group', 'style': 'we can also add another css style here'}
searchCity = StringField('View forcast of city:',
                         validators=[InputRequired("Please enter the city you want to check weather updates")],
                         render_kw=style)

但是我更希望使用第一种方法。