我正在学习Flask,我正在尝试exploreflask tutorial。我的注册表格不起作用。错误在server.py中,并说:
password = form.password.data
TypeError: __init__() got an unexpected keyword argument 'password'
我正在使用Flask-SQLAlchemy的declarative_base,所以我相信密码上的hybrid_property应该可以正常工作,但事实并非如此!
models.py:
from flask.ext.login import UserMixin
from flask.ext.bcrypt import Bcrypt
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy import Column, Integer, String
from database import Base
# Base is using Flask-SqlAlchemy's declarative_base()
class User(Base, UserMixin):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
email = Column(String(120), unique=True)
_password = Column(String(120))
def __init__(self, name=None, email=None):
self.email = email
def __repr__(self):
return '<User %r>' % (self.name)
@hybrid_property
def password(self):
return self._password
@password.setter
def _set_password(self, plaintext):
self._password = bcrypt.generate_password_hash(plaintext)
def verify_password(self, plaintext):
return bcrypt.check_password_hash(self._password, plaintext)
forms.py:
from flask_wtf import Form
from wtforms import StringField, PasswordField
from wtforms.fields.html5 import EmailField
from wtforms.validators import InputRequired
class RegisterForm(Form):
email = EmailField('email', validators=[InputRequired()])
password = PasswordField('password', validators=[InputRequired()])
server.py:
@app.route('/register', methods=["GET", "POST"])
def register():
form = RegisterForm()
if form.validate_on_submit():
user = User(
email = form.email.data,
password = form.password.data
)
db_session.add(user)
db_session.commit()
return redirect(url_for('login.html'))
return render_template('register.html', form=form)
我不确定是什么问题?
答案 0 :(得分:0)
在server.py中,您正在使用电子邮件和密码初始化User实例。 但是,User类的构造函数只接受参数name和email。
所以你应该改变它接受密码的构造函数。
def __init__(self, name=None, email=None, password=None)
#your constructor logic