Flask wtforms DecimalField不在HTML

时间:2016-09-27 21:10:17

标签: python flask wtforms flask-wtforms

我的TextField表单在html页面中显示正常,但不是DecimalFields。 DecimalFields根本不显示。

我的forms.py:

from flask.ext.wtf import Form
from wtforms import TextField, validators, IntegerField, DateField, BooleanField, DecimalField

class EnterPart(Form):
    Description = TextField(label='label', description="description", validators=[validators.required()])
    VendorCost = DecimalField(label='label',description="cost")

我的application.py:

from flask import Flask, render_template, request, redirect, url_for
def index():
    form = EnterPart(request.form) 
    return render_template('index.html', form=form)

我的index.html:

{% extends "base.html" %} {% import 'macros.html' as macros %} 
{% block content %}
{{ form.hidden_tag() }} 
{{ macros.render_field(form.Description, placeholder='Description', type='dbSerial', label_visible=false) }}
    {{ macros.render_field(form.VendorCost, placeholder='Category', type='dbSerial', label_visible=false) }}
    {% endblock %} 

我从这里的教程构建: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

提前致谢。

1 个答案:

答案 0 :(得分:0)

Macros.html没有处理DecimalField的选项。通过为DecimalField添加渲染选项,它现在可以正确显示。

我在macros.html中更改了一个块:

<div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}">
    {% if (field.type != 'HiddenField' or field.type !='CSRFTokenField') and label_visible %}
        <label for="{{ field.id }}" class="control-label">{{ field.label }}</label>
    {% endif %}
    {% if field.type == 'TextField' %}
        {{ field(class_='form-control', **kwargs) }}
    {% endif %}
    {% if field.errors %}
        {% for e in field.errors %}
            <p class="help-block">{{ e }}</p>
        {% endfor %}
    {% endif %}
</div>

为:

<div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}">
    {% if (field.type != 'HiddenField' or field.type !='CSRFTokenField') and label_visible %}
        <label for="{{ field.id }}" class="control-label">{{ field.label }}</label>
    {% endif %}
    {% if field.type == 'TextField' %}
        {{ field(class_='form-control', **kwargs) }}
    {% endif %}
    {% if field.type == 'DecimalField' %}
        {{ field(class_='form-control', **kwargs) }}
    {% endif %}
    {% if field.errors %}
        {% for e in field.errors %}
            <p class="help-block">{{ e }}</p>
        {% endfor %}
    {% endif %}
</div>