我正在通過https://realpython.com/blog/python/handling-email-confirmation-in-flask/#flask-basic-registration來構建電子郵件註冊系統。 (https://github.com/mjhea0/flask-basic-registration)TypeError:<flask_script.commands.Command對象at>:__init __()至少需要4個參數
在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="[email protected]",
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)
我在做什麼錯?
感謝Danny,我只是感到驚訝,因爲如果你看看https://realpython.com/blog/python/handling-email-confirmation-in-flask/#update-the-current-app他給了一些變量值,但不是別人 - def __init __(自我,電子郵件,密碼,已確認, paid = False,admin = False,confirmed_on =無): – user61629
@ user61629您可以使用方法簽名同時使用位置參數和關鍵字參數。只需要位置參數就可以了,關鍵字args如果沒有通過就會有默認值。 –