【问题标题】:Flask wtforms DecimalField not displaying in HTMLFlask wtforms DecimalField 未在 HTML 中显示
【发布时间】:2016-09-27 21:10:16
【问题描述】:

我的 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")

我的应用程序.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

提前致谢。

【问题讨论】:

  • macros.render_field 是做什么的?
  • 它应该为引导标准呈现字段。但你是对的,这就是问题所在。使用教程中的代码,macros.html 没有 DecimalField 的选项,这就是它根本不被渲染的原因。

标签: python flask wtforms flask-wtforms


【解决方案1】:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-08
    • 2014-07-28
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2014-02-05
    相关资源
    最近更新 更多