Oct-22-2017, 12:34 AM
(This post was last modified: Oct-22-2017, 12:34 AM by rarevesselt.)
(Oct-09-2017, 06:28 AM)buran Wrote: sorry, at the moment I can not offer advise, structure looks fine and in accordance with the recommendationThanks once again buran and others.
I haven't use flask_script and frankly for what you use it here I would simply use separate script, without link/import of app that seems to cause the problem.
As a side note - maybe it's better to migrate to new Flask CLI
I have learnt a lot on this error.
As you said,the code and structure is fine.However, it is difficult to pin point what is causing the error.I have check the code I posted earlier the one am posting now.I found no real difference.
This is the __init__.py:
import os from flask import Flask from flask_sqlalchemy import SQLAlchemy basedir = os.path.abspath(os.path.dirname(__file__)) app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI']= 'sqlite:///' + os.path.join(basedir,'thermos.db') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['DEBUG'] = True db = SQLAlchemy(app) import models import viewsand manage.py:
#! /usr/bin/env python from thermos import app,db from thermos.models import User from flask_script import Manager,prompt_bool 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()and views.py:
from flask import render_template,flash,redirect,url_for from thermos import app,db from forms import BookmarkForm from models import User,Bookmark def logged_in_user(): return User.query.filter_by(username='olatunji').first() @app.route('/index') def index(): return render_template('index.html',new_bookmarks=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 = Bookmark(user=logged_in_user(),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)The new code is running without throwing the error.