我正在通过https://realpython.com/blog/python/handling-email-confirmation-in-flask/#flask-basic-registration建立电子邮件注册系统。 (https://github.com/mjhea0/flask-basic-registration)
在models.py中有:
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String, unique=True, nullable=False)
password = db.Column(db.String, nullable=False)
registered_on = db.Column(db.DateTime, nullable=False)
admin = db.Column(db.Boolean, nullable=False, default=False)
confirmed = db.Column(db.Boolean, nullable=False, default=False)
confirmed_on = db.Column(db.DateTime, nullable=True)
def __init__(self, email, password, registered_on, admin=False, confirmed=False, confirmed_on=None):
self.email = email
self.password = bcrypt.generate_password_hash(password)
self.registered_on = datetime.datetime.now()
self.admin = admin
self.confirmed = confirmed
self.confirmed_on = confirmed_on
.....
在manage.py中,我可以运行看起来像这样的管理功能:
@manager.command
def create_admin():
"""Creates the admin user."""
db.session.add(User(
email="ad@min.com",
password="admin",
admin=True,
confirmed=True,
confirmed_on=datetime.datetime.now())
)
db.session.commit()
当我这样做时:
$ python manage.py create_admin
Traceback (most recent call last):
File "manage.py", line 78, in <module>
manager.run()
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\flask_script\__init__.py", line 412, in run
result = self.handle(sys.argv[0], sys.argv[1:])
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\flask_script\__init__.py", line 383, in handle
res = handle(*args, **config)
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\flask_script\commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "manage.py", line 30, in create_admin
confirmed_on=datetime.datetime.now())
TypeError: <flask_script.commands.Command object at 0x03530410>: __init__() takes at least 4 arguments (6 given)
我做错了什么?
答案 0 :(得分:0)
在Python中有两种类型的参数。定位参数定义如下:
def function(arg1, arg2, arg3):
和关键字参数定义如下:
def function(arg1="arg1 value", arg2="arg2 value"):
所有位置参数始终是必需的,关键字参数是可选的(如果您没有明确地将它们赋予函数,它们将默认采用定义中给定的值)。您正在尝试将位置参数作为关键字参数调用。您也未能提供所需的registered_on
位置参数。如果你在create_admin
中取出关键字并添加位置registered_on
参数,使它看起来像这样,它应该运行:
@manager.command
def create_admin():
"""Creates the admin user."""
db.session.add(User(
"ad@min.com",
"admin",
datetime.datetime.now() # this is the registered_on argument
admin=True,
confirmed=True,
confirmed_on=datetime.datetime.now())
)
db.session.commit()
如果您想更改定义而不是通话,则可以改为:
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String, unique=True, nullable=False)
password = db.Column(db.String, nullable=False)
registered_on = db.Column(db.DateTime, nullable=False)
admin = db.Column(db.Boolean, nullable=False, default=False)
confirmed = db.Column(db.Boolean, nullable=False, default=False)
confirmed_on = db.Column(db.DateTime, nullable=True)
def __init__(self, email="user@host", password="password", registered_on=datetime.datetime.now(), admin=False, confirmed=False, confirmed_on=None):
self.email = email
self.password = bcrypt.generate_password_hash(password)
self.registered_on = registered_on
self.admin = admin
self.confirmed = confirmed
self.confirmed_on = confirmed_on
.....
或者你可以做一些上述的组合,记住我对位置和关键字参数的看法。