简体   繁体   English

使用flask + wtform渲染表单

[英]rendering forms with flask + wtform

code in question: 有问题的代码:

from flask import Blueprint, render_template, abort
from flask.ext.wtf import Form
import os
from jinja2 import TemplateNotFound
from models import Member
from wtforms.ext.sqlalchemy.orm import model_form
@simple_page.route('/register')
def register():
    form = model_form(Member, Form)
    return render_template('register.html', form=form, name="bad")


class Member(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    email = db.Column(db.String(50), nullable=False, unique=True)

and in my view: 在我看来:

        <p class="txt11 colorb">
        {{ form.name }}
        </p>

this outputs <UnboundField(TextField, (), {u'default': None, u'filters': [], u'validators': [<wtforms.validators.Required object at 0x7f62f59b5590>, <wtforms.validators.Length object at 0x7f62f59b55d0>]})> , not an actual field. 这输出<UnboundField(TextField, (), {u'default': None, u'filters': [], u'validators': [<wtforms.validators.Required object at 0x7f62f59b5590>, <wtforms.validators.Length object at 0x7f62f59b55d0>]})> ,而不是实际的字段。 how do i get an actual form/field with the wtform? 如何使用wtform获得实际的表单/字段?

You're passing a form class not a form instance to the template. 您正在将表单而不是表单实例传递给模板。 The model_form method generates a new class dynamically, which can be re-used, extended, and otherwise used like any other form subclass. model_form方法动态生成一个新类,可以像任何其他表单子类一样重复使用,扩展和使用。 It's also quite un-necessary to generate this form class on every run of your view, so you can move this call outside of your view. 在视图的每次运行中生成此表单类也非常不必要,因此您可以将此调用移到视图之外。

You passing an uninstantiated class is also why you get strange behavior relating to UnboundField (which is how WTForms handles declarative field instantiation) 您传递未实例化的类也是您获得与UnboundField相关的奇怪行为的原因(这是WTForms处理声明性字段实例化的方式)

The fix is simple: 修复很简单:

MemberForm = model_form(Member, base_class=Form)

@simple_page.route('/register')
def register():
    form = MemberForm(name=u'bad')
    return render_template('register.html', form=form)

You need to do a bit more work: 你需要做更多的工作:

{{ form.name.label }} : {{ form.name()|safe }}

Or, you can use this handy snippet : 或者,您可以使用这个方便的代码段

{% macro render_field(field) %}
  <dt>{{ field.label }}
  <dd>{{ field(**kwargs)|safe }}
  {% if field.errors %}
    <ul class=errors>
    {% for error in field.errors %}
      <li>{{ error }}</li>
    {% endfor %}
    </ul>
  {% endif %}
  </dd>
{% endmacro %}

Of course, adjust the HTML rendered. 当然,调整HTML渲染。 Save that file somewhere in your templates directory and then in your main template. 将该文件保存在templates目录中的某个位置,然后保存在主模板中。 Here its formhelpers.html : 这是formhelpers.html

{% from "formhelpers.html" import render_field %}
<form method=post action="/register">
  <dl>
    {{ render_field(form.name) }}
    {{ render_field(form.email) }}
  </dl>
  <p><input type=submit value=Register>
</form>

I know this is pretty old, but I had the same problem and I want to share my solution for futures needs. 我知道这已经很老了,但我有同样的问题,我想分享我的期货需求解决方案。

I also got the html rendered by "UnboundField". 我还得到了“UnboundField”渲染的html。 After fighting with the code I found out i'm using: 在与我发现我使用的代码打架后:

from wtforms import Form

And it's looks good, but when using Flask I had to do this: 它看起来不错,但是当使用Flask时我必须这样做:

from flask.ext.wtf import Form

Fixed my problem. 解决了我的问题。 Hope it's helped 希望它有所帮助

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

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