Python中文
首页
教程
问答
标签
搜索
登录
注册
当我在Flas中使用POST时,wtforms selectfields出现问题
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我对wtforms和flask还不太熟悉,在处理selectfields时出错了。表单本身在没有selectfield的情况下工作得很好,但是有了它,我会得到以下错误:</p> <p><strong>错误:</strong></p> <pre><code>....fields.py", line 386, in pre_validate for v, _ in self.choices: TypeError: 'NoneType' object is not iterable </code></pre> <p>我看到了selectfield,所以它正在被渲染。我怀疑在POST上这个id没有被正确地验证,并且没有返回任何信息。或者跟我的selectfield元组被返回有关?另外,我使用的ID字段是从GAE的ndb automatic key().ID()中提取的,它相当长而且令人讨厌。可能是selectfield使用的id长度太长?</p> <p>谷歌并没有提供太多的具体问题,所以我想我会在这里发帖。相关代码如下。如果我遗漏了什么,请告诉我</p> <p><strong>views.py代码:</strong></p> <pre><code>@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) </code></pre> <p><strong>myforms.py</strong>:</p> <pre><code>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()]) </code></pre> <p><strong>新文章.html:</strong></p> <pre><code>{% 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 %} </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>您应该提供<code>choices=[...]</code>参数,比如</p> <pre><code>wtf.SelectField(u'Home Name List', choices=[(1, 'Label 1'), (2, 'Label 2')], coerce=int, validators=[validators.optional()]) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
将Pandas数据帧转换为PyTorch张量?
4 回答
将Pandas数据帧转换为scipy稀疏矩阵
2 回答
将Pandas数据帧转换为Spark Datafram时出现问题
10 回答
将pandas数据帧转换为spark DataFram时出错
1 回答
将Pandas数据帧转换为spark datafram时收到错误
1 回答
将Pandas数据帧转换为Spark数据帧
5 回答
将Pandas数据帧转换为Tensorflow数据
2 回答
将Pandas数据帧转换为tkinter obj
1 回答
将pandas数据帧转换为XML
4 回答
将Pandas数据帧转换为值sql语句
9 回答
将pandas数据帧转换为元组
10 回答
将pandas数据帧转换为元组列表
3 回答
将pandas数据帧转换为元组列表并删除所有pandas数据类型
2 回答
将pandas数据帧转换为具有头和数据类型的numpy数组
6 回答
将pandas数据帧转换为内存中的拼花地板,并将其加载到Python中的Hadoop中
7 回答
将pandas数据帧转换为内存中类似文件的对象?
8 回答
将Pandas数据帧转换为内存功能(&F)
2 回答
将pandas数据帧转换为列表列表
2 回答
将pandas数据帧转换为列表列表以输入到RNN
8 回答
将Pandas数据帧转换为单行DataFram
2 回答