[英]Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'db' is not defined
''' import db.py from flask import Flask, render_template from flask_sqlalchemy import SQLAlchemy from datetime import datetime ''' import db.py from flask import Flask, render_template from flask_sqlalchemy import SQLAlchemy from datetime import datetime
app =Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///posts.db'
class Question(db.model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
content = db.Column(db,Text, nullable=False)
Date_Posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
def __repr__(self):
return 'Question' + str(self.id)
@app.route('/mainpage' , methods = ['GET'])
def mainpage():
return render_template("main.html")
@app.route('/askdaiwik')
def ask():
return render_template("ask.html" , ask = all_ask)
if __name__ == "__main__":
app.run(debug=True)
''' '''
If I'm not wrong you are trying to use SQLAlchemy
object as db
.如果我没记错的话,您正在尝试使用SQLAlchemy
object 作为db
。 But you didn't create the object and hence db
is undefined.但是您没有创建 object,因此db
未定义。
wrap your app with SQLAlchemy
to create the object and assign the variable db
to the instance.使用SQLAlchemy
包装您的应用程序以创建 object 并将变量db
分配给实例。
db = SQLAlchemy(app)
In db.py where you can store the code db = SQLAlchemy() .在 db.py 中,您可以在其中存储代码db = SQLAlchemy() 。
Then import it in app.py.然后在app.py中导入。 Now you can call db.现在你可以调用db.
or just remove APP in db=SQLAlchemy(app)或者只是在db=SQLAlchemy(app)中删除 APP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.