我一直在尝试建立这个烧瓶登录系统,但不断收到此错误。我查找了特定的错误,这些建议似乎不适合这个问题。
我在模板中取出了两个(电子邮件和名称)字段并且它有效,但我不确定为什么我想你应该能够拥有任意数量的字段。此外,它不会将用户数据提交到数据库。
from flask import Flask, render_template, redirect
from flask_wtf import FlaskForm
from wtforms import BooleanField, StringField, PasswordField
from wtforms.validators import InputRequired, Length, Email
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = 'sosecretbro'
app.config['SQLALCHEMY_DATABASE_URI']= 'sqlite:////Users/myname/Desktop/officeplace/database.db'
Bootstrap(app)
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column('id', db.Integer(), primary_key=True)
name = db.Column('name', db.String(50))
username = db.Column('username', db.String(20), unique=True)
password = db.Column('password', db.String(80), unique=True)
email = db.Column('email', db.String(50))
class RegisterForm(FlaskForm):
name = StringField('name', validators=[InputRequired(), Length(min=3, max=50)])
username = StringField('username', validators=[InputRequired(), Length(min=8, max=15)])
password = StringField('password', validators=[InputRequired(), Length(min=8, max=80)])
email = StringField('email', validators=[InputRequired, Email(message=None)])
class LoginForm(FlaskForm):
username = StringField('username', validators=[InputRequired(), Length(min=8, max=15)])
password = StringField('password', validators=[InputRequired(), Length(min=8, max=80)])
@app.route('/')
def index():
return 'We here now'
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm()
if form.validate_on_submit():
new_user = User(username=form.username.data, password=form.password.data)
db.session.add(new_user)
db.session.commit()
return '<h1>' 'New user created' '<h1>'
return render_template('register.html', form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
return render_template('login.html', form=form)
if (__name__) == '__main__':
app.run(debug = True)
模板 -
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}
Register Here
{% endblock %}
{% block styles %}
{{super()}}
<link rel="stylesheet" href="{{url_for('.static', filename='register.css')}}">
{% endblock %}
{% block content %}
<div class="container">
<form class="form-register" method="POST" action="/register">
<h2 class="form-register-heading">Register, Fool.</h2>
{{ form.hidden_tag() }}
{{ wtf.form_field(form.name) }}
{{ wtf.form_field(form.username) }}
{{ wtf.form_field(form.password) }}
{{ wtf.form_field(form.email) }}
<button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
</form>
</div> <!-- /container -->
{% endblock %}
追溯 -
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/nicksmith/Desktop/officeplace/officeplace.py", line 43, in register
if form.validate_on_submit():
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/flask_wtf/form.py", line 101, in validate_on_submit
return self.is_submitted() and self.validate()
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/wtforms/form.py", line 310, in validate
return super(Form, self).validate(extra)
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/wtforms/form.py", line 152, in validate
if not field.validate(self, extra):
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/wtforms/fields/core.py", line 204, in validate
stop_validation = self._run_validation_chain(form, chain)
File "/Users/nicksmith/Desktop/officeplace/lib/python3.6/site-packages/wtforms/fields/core.py", line 224, in _run_validation_chain
validator(form, self)
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given