def main(): app = create_app(DATABASE_URI, debug=True) app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI app.secret_key ='123' db.init_app(app) login_manager.init_app(app) login_manager.login_view = 'login' manager.add_command('db', MigrateCommand) manager.run()
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" app = Flask(__name__.split('.')[0], instance_relative_config=True) app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY='dev', # store the database in the instance folder DATABASE=os.path.join(app.instance_path, 'blog.sqlite'), ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass @app.route('/hello') def hello(): return 'Hello, World!' # register the database commands from blog import db db.init_app(app) # apply the blueprints to the app from blog import auth, my_blog app.register_blueprint(auth.bp) app.register_blueprint(my_blog.bp) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index app.add_url_rule('/', endpoint='index') return app
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY="dev", DATABASE="blog", ) if test_config is None: # load the instance config, if it exists, when not testing # if silent=False (default), raise if file does not exist app.config.from_pyfile("config.py", silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # register the database commands with app.app_context(): from blog import db db.init_app(app) # apply the blueprints to the app # from blog import blog from blog import api # app.register_blueprint(auth.bp) # app.register_blueprint(blog.bp) app.register_blueprint(api.bp) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index # app.add_url_rule("/", endpoint="index") return app
''' Runs our blog engine. Register blueprints, configurations and initialize exetensions here. Clears cache before launching app.''' from flask.ext.uploads import configure_uploads, patch_request_class from blog import app, login_manager, md, admin, db, photos, cache from blog.posts.models import Post from blog.admin.posts import posts app.config.from_object('config') app.register_blueprint(posts) # initialize extensions configure_uploads(app, photos) patch_request_class(app, 32 * 1024 * 1024) login_manager.init_app(app) md.init_app(app) db.init_app(app) cache.init_app(app, config=app.config) with app.app_context(): cache.clear() if __name__ == '__main__': app.run(debug=True)
from flask import Flask from blog.models import User, Post from blog import db import pymysql app = Flask(__name__) #app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///siteqa.db' app.config[ 'SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:mysql@localhost/blog' db.init_app(app) app.app_context().push() db.create_all() # Create all blank tables user1 = User(username='******', email='*****@*****.**', password='******') db.session.add(user1) db.session.commit() User.query.all()