V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
juventusryp
V2EX  ›  Python

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

  •  
  •   juventusryp · 2016-10-05 11:21:26 +08:00 · 6556 次点击
    这是一个创建于 2963 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在学习 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
    
    
    5 条回复    2016-10-06 12:01:19 +08:00
    XYxe
        1
    XYxe  
       2016-10-05 11:54:12 +08:00   ❤️ 1
    name = StringField('What is your name?',validators=[Required()])
    arischow
        2
    arischow  
       2016-10-05 12:07:23 +08:00 via iPhone
    先不用插件实现这些功能,会有更好的理解
    juventusryp
        3
    juventusryp  
    OP
       2016-10-05 12:24:37 +08:00
    @XYxe 果然是这个问题,我好粗心,十分感谢!
    loading
        4
    loading  
       2016-10-05 15:18:27 +08:00 via Android
    同建议不用插件
    jackie9692
        5
    jackie9692  
       2016-10-06 12:01:19 +08:00
    python 函数跟类名得注意,在 ORM 有遇到过 Column()与 column 的坑,一般变量名首字母大写,是类名
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1077 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 22:53 · PVG 06:53 · LAX 14:53 · JFK 17:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.