当我在Flask中使用POST时,wtforms selectfields出现问题 [英] I'm having problems with wtforms selectfields when i use a POST with Flask

查看:80
本文介绍了当我在Flask中使用POST时,wtforms selectfields出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对wtforms和flask很陌生,并且在选择字段周围乱七八糟,并得到了一个错误.表单本身在没有selectfield的情况下也可以正常工作,但是出现以下错误:

I'm pretty new to wtforms and flask and was messing around with selectfields and got an error. The form itself works just fine without the selectfield but with it I get the following error:

错误:

....fields.py", line 386, in pre_validate
    for v, _ in self.choices: TypeError: 'NoneType' object is not iterable

我看到了选择域,因此它正在呈现.我怀疑以某种方式无法在POST上正确验证ID,并且未返回任何ID.还是与返回的selectfield元组有关?另外,我正在使用的ID字段是从GAE的ndb自动key().id()中提取的,它相当长且令人讨厌.可能是用于selectfield的id长度太长吗?

I see the selectfield so it's being rendered. I suspect somehow the id is not being validated properly on POST and is returning none. Or it has something to do with my selectfield tuple being returned ? Also the ID field I'm using is pulled from GAE's ndb automatic key().id() which is rather long and obnoxious. It could be the id length being used for the selectfield is too long ?

在确切问题方面,Google搜寻功能不多,因此我认为我会在此处发布.相关代码如下.如果我缺少什么,请告诉我

Googling hasn't provided much in terms of the exact problem so thought I'd post here. Relevant code below. If I'm missing something please let me know

views.py代码:

@app.route('/new/post', methods = ['GET', 'POST'])
@login_required
def new_post():

    form = PostForm()
    if form.validate_on_submit():
        post = Post(title = form.title.data,
                    content = form.content.data,
                    hometest = form.hometest.data,
                    author = users.get_current_user())
        post.put()
        flash('Post saved on database.')
        return redirect(url_for('list_posts'))
    form.hometest.choices = [ (h.key.id(),h.homename)for h in Home.query()]

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

myforms.py :

class PostForm(Form):
    title = wtf.TextField('Title', validators=[validators.Required()])
    content = wtf.TextAreaField('Content', validators=[validators.Required()])
    hometest = wtf.SelectField(u'Home Name List', coerce=int,validators=[validators.optional()])

new_post.html:

{% extends "base.html" %}

{% block content %}
    <h1 id="">Write a post</h1>
    <form action="{{ url_for('new_post') }}" method="post" accept-charset="utf-8">
        {{ form.csrf_token }}
        <p>
            <label for="title">{{ form.title.label }}</label><br />
            {{ form.title|safe }}<br />
            {% if form.title.errors %}
            <ul class="errors">
                {% for error in form.title.errors %}
                <li>{{ error }}</li>
                {% endfor %}
            </ul>
            {% endif %}
        </p>
        <p>
            <label for="title">{{form.hometest.label}}</label><br/>
            {{form.hometest}}
            {% if form.hometest.errors %}
        <ul class="errors">
            {% for error in form.hometest.errors %}
            <li>{{ error }}</li>
            {% endfor %}
        </ul>
        {% endif %}
        </p>
        <p>
            <label for="title">{{ form.content.label }}</label><br />
            {{ form.content|safe }}<br />

            {% if form.content.errors %}
            <ul class="errors">
                {% for error in form.content.errors %}
                <li>{{ error }}</li>
                {% endfor %}
            </ul>
            {% endif %}
        </p>
        <p><input type="submit" value="Save post"/></p>
    </form>
{% endblock %}

推荐答案

在调用validate_on_submit之前,需要先设置,因为form.validate会尝试验证提供的值(如果有) )与选项列表(在设置choices之前为None)相对应:

You need to set your choices before you call validate_on_submit as form.validate will attempt to validate the provided value (if any) against the list of choices (which is None before you set choices):

form = PostForm()
form.hometest.choices = [(h.key.id(), h.homename) for h in Home.query()]

if form.validate_on_submit():
    # form is valid, continue

这篇关于当我在Flask中使用POST时,wtforms selectfields出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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