名称错误:未定义名称'datetime' 37



我在Ubuntu中使用虚拟盒子,当尝试运行下面的命令时,我出现了这个错误,知道发生了什么吗?你可以在代码下面看得更清楚!!

代码:

(flaskenv) argiris@argiris-VirtualBox:~/myprojects/FlaskIntro$ python3 app.py

错误:

/home/argiris/myprojects/FlaskIntro/flaskenv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py:833: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
warnings.warn(FSADeprecationWarning(
Traceback (most recent call last):
File "app.py", line 8, in <module>
class Todo(db.Model):
File "app.py", line 11, in Todo
date_created = db.Column(db.DateTime, default=datetime.utcnow)
NameError: name 'datetime' is not defined

代码段:

(flaskenv) argiris@argiris-VirtualBox:~/myprojects/FlaskIntro$ vim app.py
from flask import Flask, render_template, url_for
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)

class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(200), nullable=False)
date_created = db.Column(db.DateTime, default=datetime.utcnow)
date_completed = db.Column(db.DateTime, default=date(2000,1,1))

def __repr__(self):
return '<Task %r>' % self.id


@app.route('/')
def index():
return render_template('index.html')
if __name__ == "__main__":
app.run(debug=True)

您需要导入日期时间

from datetime import datetime

相关内容

  • 没有找到相关文章

最新更新