学习 flask web 开发,到第四章遇到 validate_on_submit 问题

2016-10-05 11:21:26 +08:00
 juventusryp

在学习 flask web 开发这本书的时候,在第四章,遇到了一个问题。

按照书上, hello.py 代码如下:

from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from datetime import datetime
from flask_wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required


app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
bootstrap = Bootstrap(app)
moment = Moment(app)

class NameForm(Form):
    name = StringField('What is your name?',validators=[Required])
    submit = SubmitField('Submit')

@app.route('/', methods=['GET', 'POST'])
def index():
    name = None
    form = NameForm()
    if form.validate_on_submit():
        name = form.name.data
        form.name.data = ''
    return render_template('index.html',name=name,form=form)

index.html 代码如下:

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block title %}Flasky{% endblock %}

{% block page_content %}
<div class="page-header">
   <h1>{% if name %}Hello,{{ name }}{% else %}Hello,Stranger{% endif %}!</h1>
</div>
{{ wtf.quick_form(form) }}
{% endblock %}

之后在浏览器表单里输入内容提交后,会报错:

Traceback (most recent call last):
  File "/Users/lovehxy/.virtualenvs/flask/lib/python3.4/site-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/lovehxy/.virtualenvs/flask/lib/python3.4/site-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/lovehxy/.virtualenvs/flask/lib/python3.4/site-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/lovehxy/.virtualenvs/flask/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/lovehxy/.virtualenvs/flask/lib/python3.4/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/lovehxy/.virtualenvs/flask/lib/python3.4/site-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/lovehxy/Desktop/flask study/hello.py", line 24, in index
    if form.validate_on_submit():
  File "/Users/lovehxy/.virtualenvs/flask/lib/python3.4/site-packages/flask_wtf/form.py", line 156, in validate_on_submit
    return self.is_submitted() and self.validate()
  File "/Users/lovehxy/.virtualenvs/flask/lib/python3.4/site-packages/wtforms/form.py", line 271, in validate
    return super(Form, self).validate(extra)
  File "/Users/lovehxy/.virtualenvs/flask/lib/python3.4/site-packages/wtforms/form.py", line 130, in validate
    if not field.validate(self, extra):
  File "/Users/lovehxy/.virtualenvs/flask/lib/python3.4/site-packages/wtforms/fields/core.py", line 175, in validate
    stop_validation = self._run_validation_chain(form, chain)
  File "/Users/lovehxy/.virtualenvs/flask/lib/python3.4/site-packages/wtforms/fields/core.py", line 195, in _run_validation_chain
    validator(form, self)
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given

6586 次点击
所在节点    Python
5 条回复
XYxe
2016-10-05 11:54:12 +08:00
name = StringField('What is your name?',validators=[Required()])
arischow
2016-10-05 12:07:23 +08:00
先不用插件实现这些功能,会有更好的理解
juventusryp
2016-10-05 12:24:37 +08:00
@XYxe 果然是这个问题,我好粗心,十分感谢!
loading
2016-10-05 15:18:27 +08:00
同建议不用插件
jackie9692
2016-10-06 12:01:19 +08:00
python 函数跟类名得注意,在 ORM 有遇到过 Column()与 column 的坑,一般变量名首字母大写,是类名

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/310636

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX