0
這是我嘗試在本課程中使用「python manage.py dropdb」時發生的錯誤,在https://app.pluralsight.com/library/courses/flask-micro-framework-introduction/table-of-contents處。 代碼是確切的,但每當我運行上面的命令時都會出現此導入錯誤。導入錯誤:無法導入名稱db
Traceback (most recent call last):
File "manage.py", line 2, in <module>
from thermos import app,db
File "/Users/tunji/dev/thermos/thermos/thermos.py", line 7, in <module>
import models
File "/Users/tunji/dev/thermos/thermos/models.py", line 5, in <module>
from thermos import db
這是我thermos.py:
import os
from flask import Flask,render_template,url_for, flash,redirect
from flask_sqlalchemy import SQLAlchemy
from forms import BookmarkForm
import models
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI']= 'sqlite:///' + os.path.join(basedir,'thermos.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
def new_bookmarks(num):
return []
@app.route('/index')
def index():
return render_template('index.html',new_bookmarks=models.Bookmark.newest(5))
@app.route("/add",methods=['GET','POST'])
def add():
form = BookmarkForm()
if form.validate_on_submit():
url = form.url.data
description =form.description.data
bm = models.Bookmark(url=url,description=description)
db.session.add(bm)
db.session.commit()
flash("Stored '{}'".format(description))
return redirect(url_for('index'))
return render_template('add.html',form=form)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'),404
@app.errorhandler(500)
def server_error(e):
return render_template('500.html'),500
if __name__ == '__main__':
app.run(debug=False)
和我manage.py:
#! /usr/bin/env python
from thermos import app,db
from flask_script import Manager,prompt_bool
from thermos import db
from models import User
manager = Manager(app)
@manager.command
def initdb():
db.create_all()
db.session.add(User(username='olatunji',email='[email protected]'))
db.session.add(User(username='ayobami',email='[email protected]'))
db.session.commit()
print "Initialized the database"
@manager.command
def dropdb():
if prompt_bool(
"Are you sure you want to lose all your data"):
db.drop_all()
print 'Dropped the database'
if __name__ == '__main__':
manager.run()
這是目錄結構:
./thermos:
__init__.py manage.py static thermos.py
forms.py models.py templates thermos.pyc
forms.pyc models.pyc thermos.db
./thermos/static:
css img js
./thermos/static/css:
main.css normalize.css normalize.min.css
./thermos/static/img:
./thermos/static/js:
main.js vendor
./thermos/static/js/vendor:
jquery-1.11.2.min.js modernizr-2.8.3-respond-1.4.2.min.js
./thermos/templates:
404.html add.html form_macros.html
500.html base.html index.html
我怎樣才能解決這個回溯中的錯誤?任何幫助將是一個ppreciated。