简体   繁体   中英

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

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 . But you didn't create the object and hence db is undefined.

wrap your app with SQLAlchemy to create the object and assign the variable db to the instance.

db = SQLAlchemy(app) 

In db.py where you can store the code db = SQLAlchemy() .

Then import it in app.py. Now you can call db.

or just remove APP in db=SQLAlchemy(app)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM