简体   繁体   English

回溯(最近调用最后):文件“<stdin> ",第 1 行,在<module> NameError: 名称 'db' 未定义</module></stdin>

[英]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.

相关问题 获取回溯(最近一次调用最后一次):文件“<stdin> &quot;,第 1 行,在<module> NameError:名称“文件名”未定义 - Getting Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'filename' is not defined 回溯(最近调用最后):文件“<stdin> “,第 1 行,在<module> NameError:名称“游泳”未定义</module></stdin> - Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'swim' is not defined 无法在python 3Traceback(最近一次调用为最新)中找出此错误:“文件” <stdin> ”,第1行,在 <module> NameError:未定义名称“ xlrd” - Cannot figure out this error in python 3Traceback (most recent call last): File “<stdin>”, line 1, in <module> NameError: name 'xlrd' is not defined Traceback(最近一次调用最后一次):,第 131 行,在<module> print(self.marka) NameError: name 'self' is not defined</module> - Traceback (most recent call last): , line 131, in <module> print(self.marka) NameError: name 'self' is not defined 追溯(最近一次通话):“文件” <stdin> ”,第1行,在 <module> - Traceback (most recent call last): File “<stdin>”, line 1, in <module> 追溯(最近一次通话最后一次):NameError:未定义名称“路径” - Traceback (most recent call last):NameError: name 'path' is not defined Traceback(最近一次调用最后一次):文件“<stdin> ”,第 1 行,在<module> ModuleNotFoundError:没有名为“Webhook”的模块</module></stdin> - Traceback (most recent call last): File “<stdin>”, line 1, in <module> ModuleNotFoundError: No module named 'Webhook' Traceback(最近一次通话最后一次):文件“<stdin> &quot;,第 1 行,在<module> TypeError: object() 没有参数 - Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object() takes no parameters 我可以逃避此错误回溯(最近一次通话是最近一次):“文件” <stdin> ”,第1行,在 <module> 档案“ <stdin> ”,第2行,在data_entry中 - can i escape this error Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “<stdin>”, line 2, in data_entry 为什么“回溯(最近一次通话最后一次):文件”<stdin> ”,第 1 行,在<module> ImportError:当我尝试安装 django 时,没有名为 django 的模块?</module></stdin> - why 'Traceback (most recent call last): File “<stdin>”, line 1, in <module> ImportError: No module named django' when im trying to install django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM