2017-10-19 6 views
0

これは、このコースの "python manage.py dropdb"をhttps://app.pluralsight.com/library/courses/flask-micro-framework-introduction/table-of-contentsで使用しようとしているときのエラーです。 コードは正確ですが、上記のコマンドを実行するたびにこのインポートエラーが発生しています。インポートエラー:名前のデータベースをインポートできません

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。

答えて

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) 
関連する問題