如何使用Flask修复无法找到的TypeError

时间:2019-04-20 12:03:46

标签: python flask

我发现了Flask框架,并且正在构建自己的网站。 请放心,我是初学者。 我用wtforms的StringField(下面的代码)创建了一个注册页面,并用SubmitField创建了一个“登录”按钮。

填写完字段后,我单击“登录”按钮,我的网站给我这个错误:

  

TypeError: init ()接受1到2个位置参数,但3个   被给予

我试图了解我的TypeError()在哪里。因此,我已经搜索了所有.html,.py文件,但没有找到崩溃的地方。因此,如果我了解一个 init ()函数,该函数需要1或2个参数,而我有3个参数。那就对了 ?但是我的任何文件内都没有任何 init ()函数。那么它来自导入的库吗?或来自Flask?

追溯:

Traceback (most recent call last):
  File "/home/tt/.local/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/tt/.local/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/tt/.local/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/tt/.local/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/tt/.local/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/tt/.local/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/tt/.local/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/tt/.local/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/tt/.local/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/tt/.local/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/tt/Desktop/Flask/flask_site.py", line 29, in register
    if form.validate_on_submit():
  File "/home/tt/.local/lib/python3.6/site-packages/flask_wtf/form.py", line 101, in validate_on_submit
    return self.is_submitted() and self.validate()
  File "/home/tt/.local/lib/python3.6/site-packages/wtforms/form.py", line 310, in validate
    return super(Form, self).validate(extra)
  File "/home/tt/.local/lib/python3.6/site-packages/wtforms/form.py", line 152, in validate
    if not field.validate(self, extra):
  File "/home/tt/.local/lib/python3.6/site-packages/wtforms/fields/core.py", line 206, in validate
    stop_validation = self._run_validation_chain(form, chain)
  File "/home/tt/.local/lib/python3.6/site-packages/wtforms/fields/core.py", line 226, in _run_validation_chain
    validator(form, self)
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given

我的表格。py:

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField, validators
from wtforms.validators import DataRequired, Length, Email, EqualTo

class RegistrationForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    email = StringField('Email', validators=[DataRequired(), Email])
    password = PasswordField('Password', validators=[DataRequired()])
    confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('Sign Up')

class LoginForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Email])
    password = PasswordField('Password', validators=[DataRequired()])
    remember = BooleanField('Remember Me')
    submit = SubmitField('Login')

我的main.py:

from flask import Flask, render_template, url_for, flash, redirect
from forms import RegistrationForm, LoginForm
app = Flask(__name__)

app.config['SECRET_KEY'] = '****************************'


@app.route("/home")
def home():
    return render_template('home.html', title='Home')

@app.route("/home/register", methods=['GET','POST'])
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        flash(f'Account Created for {form.username.data}!', 'success')
        return redirect(url_for('home'))
    return render_template('register.html', title='Register', form=form)

@app.route("/login")
def login():
    form = LoginForm()
    return render_template('login.html', title='login', form=form)



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

0 个答案:

没有答案