wtforms 隐藏字段值 [英] wtforms hidden field value

查看:28
本文介绍了wtforms 隐藏字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WTForms,我有一个隐藏字段不返回值的问题,而文档说他们应该.这是一个简单的例子:

I am using WTForms, and I have a problem with hidden fields not returning values, whereas the docs say they should. Here's a simple example:

forms.py:

from wtforms import (Form, TextField, HiddenField)

class TestForm(Form):
    fld1 = HiddenField("Field 1")
    fld2 = TextField("Field 2")

experiment.html:

{% from "_formshelper.html" import render_field %}
<html>
    <body>
        <table>
        <form method=post action="/exp">
            {% for field in form %}
                {{ render_field(field) }}
            {% endfor %}
            <input type=submit value="Post">
        </form>
        </table>        
    </body>
</html>

(render_field 只是将标签、字段和错误放在 td 标签中)

(render_field just puts the label, field and errors in td tags)

experiment.py:

from flask import Flask, request, render_template

from templates.forms import *
from introspection import *

app = Flask(\__name__)                  
app.config.from_object(\__name__)
db_session = loadSession()

@app.route('/exp', methods=['POST', 'GET'])
def terms():
    mydata = db_session.query(Peter).one()
    form = TestForm(request.form, mydata)
    if request.method == 'POST' and form.validate():
        return str(form.data)
    return render_template('experiment.html', form = form)

if __name__ == '__main__':
    app.run(debug = True)  

mydata 返回具有 2 个字段 fld1 和 fld2 的表中的唯一行.fld1 是一个整数自动增量字段.表单填充了这些数据,所以如果我运行experiment.py,当我提交表单时,我得到:

mydata returns the only row from a table that has 2 fields, fld1 and fld2. fld1 is an integer autoincrement field. The form is populated with that data, so if I run experiment.py, when I submit the form I get:

{'fld2': u'blah blah blah', 'fld1': u'1'}

{'fld2': u'blah blah blah', 'fld1': u'1'}

但是如果我将 fld1 更改为 HiddenField,当我点击提交时,我会得到:{'fld2': u'blah blah blah', 'fld1': u''}

But if I change fld1 to HiddenField, when I hit submit, I get: {'fld2': u'blah blah blah', 'fld1': u''}

我做错了什么?

推荐答案

我怀疑您的隐藏字段要么 (1) 未获得值集,要么 (2) render_field 宏未正确构建它.如果我不得不打赌,我会说您的mydata"对象没有您期望的值.

I suspect your hidden field is either (1) not getting a value set, or (2) the render_field macro isn't building it correctly. If I had to bet, I'd say your "mydata" object doesn't have the values you expect.

我将您的代码精简到最低限度,这对我有用.请注意,我明确为两个字段赋值:

I stripped your code down to the bare minimum, and this works for me. Note I am explicitly giving a value to both fields:

from flask import Flask, render_template, request
from wtforms import Form, TextField, HiddenField

app = Flask(__name__)

class TestForm(Form):
  fld1 = HiddenField("Field 1")
  fld2 = TextField("Field 2")


@app.route('/', methods=["POST", "GET"])
def index():
  form = TestForm(request.values, fld1="foo", fld2="bar")
  if request.method == 'POST' and form.validate():
    return str(form.data)

  return render_template('experiment.html', form = form)

if __name__ == '__main__':
  app.run()

<html>
<body>
<table>
    <form method=post action="/exp">
        {% for field in form %}
            {{field}}
        {% endfor %}
        <input type=submit value="Post">
    </form>
</table>
</body>
</html>

正如我所料,这给了我 {'fld2': u'bar', 'fld1': u'foo'}.

This gives me {'fld2': u'bar', 'fld1': u'foo'} as I would expect.

检查 mydata 是否有一个属性fld1"并且它有一个值.我可能会像 form = TestForm(request.values, obj=mydata) 一样明确地设置它 - 它看起来不像 WTForms 会关心,但我有时会被它奇怪的挑剔所烧毁.

Check that mydata has an attribute "fld1" and it has a value. I might set it explicitly like form = TestForm(request.values, obj=mydata) - it doesn't look like WTForms would care, but I've gotten burned by it being weirdly picky sometimes.

如果这对您不起作用,请返回并发布您的 HTML 以及 mydata 的值.

If that doesn't work for you, come back and post your HTML and what values mydata has.

这篇关于wtforms 隐藏字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆