NameError:未定义名称“ datetime” 37

时间:2020-09-22 13:29:25

标签: python mysql linux ubuntu

我在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)

1 个答案:

答案 0 :(得分:0)

您需要导入日期时间

from datetime import datetime