2016-09-27 54 views
-1

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

我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

什么'macros.render_field'吗? – reptilicus

+0

它应该为引导标准渲染领域。但是你是对的,这就是问题出在哪里。使用教程中的代码,macros.html没有DecimalField的选项,这就是为什么它没有被渲染。 – Generacy

回答

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> 
相关问题