这是我在https://app.pluralsight.com/library/courses/flask-micro-framework-introduction/table-of-contents课程中尝试使用“python manage.py dropdb”时遇到的错误。 代码是准确的,但每当我运行上面的命令时都会出现这个导入错误。
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='ayo19602003@yahoo.com'))
db.session.add(User(username='ayobami',email='bambamaks37@gmail.com'))
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
如何解决在追溯中解决此错误的问题?我们将不胜感激。
答案 0 :(得分:0)
我对我的thermos.py做了一点调整:
import os
from flask import Flask,render_template,url_for, flash,redirect
from flask_sqlalchemy import SQLAlchemy
from forms import BookmarkForm
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)
import models
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)