Python Web App使用Flask," app未定义"在Heroku上

时间:2016-01-19 02:56:54

标签: python heroku flask flask-login

我正在尝试在heroku上运行一个简单的应用程序,但由于某些我不完全理解的原因,当我推送并尝试在heroku上运行时,app不是由下面的代码定义的。当我在本地运行此代码时,使用python myApp.py工作正常。我得到" app未定义"。我似乎需要在create app方法之外声明app = Flask(__name__)。我从flask-user文档中获得了这段代码。我如何让它工作,以便我可以在create_app方法中定义app,以便与heroku一起运行?

我的要求如下,可能不会在这里全部使用:

['babel==2.2.0', 'bcrypt==2.0.0', 'blinker==1.4', 'cffi==1.5.0', 'flask-babel==0.9',
 'flask-login==0.3.2', 'flask-mail==0.9.1', 'flask-sqlalchemy==2.1', 'flask-user==0.6.8',
 'flask-wtf==0.12', 'flask==0.10.1', 'gunicorn==19.4.1', 'itsdangerous==0.24', 
'jinja2==2.8', 'markupsafe==0.23', 'mysql-python==1.2.5', 'mysql==0.0.1', 
'passlib==1.6.5', 'pip==7.1.2', 'pycparser==2.14', 'pycrypto==2.6.1', 
'pymysql==0.7.1', 'pytz==2015.7', 'setuptools==18.3.1', 'six==1.10.0', 
'speaklater==1.3', 'sqlalchemy==1.0.11', 'vboxapi==1.0', 'virtualenv==13.1.2', 
'werkzeug==0.11.3', 'wheel==0.24.0', 'wtforms==2.1']
 import os
    from flask import Flask, redirect, render_template_string, request, url_for
    from flask_babel import Babel
    from flask_mail import Mail
    from flask_sqlalchemy import SQLAlchemy
    from flask_user import confirm_email_required, current_user, login_required, UserManager, UserMixin, SQLAlchemyAdapter




# Use a Class-based config to avoid needing a 2nd file
# os.getenv() enables configuration through OS environment variables
class ConfigClass(object):
  ....


def create_app(test_config=None):                   # For automated tests
    # Setup Flask and read config from ConfigClass defined above
    app = Flask(__name__)
    app.config.from_object(__name__+'.ConfigClass')

    # Load local_settings.py if file exists         # For automated tests
    try: app.config.from_object('local_settings')
    except: pass

    # Load optional test_config                     # For automated tests
    if test_config:
        app.config.update(test_config)

    # Initialize Flask extensions
    mail = Mail(app)                                # Initialize Flask-Mail
    db = SQLAlchemy(app)                            # Initialize Flask-SQLAlchemy

    # Define the User data model. Make sure to add flask.ext.user UserMixin!!
    class User(db.Model, UserMixin):
     ....

    # Define the Role data model
    class Role(db.Model):
    ....


    # Define the UserRoles data model
    class UserRoles(db.Model):
      ....


    db_adapter = SQLAlchemyAdapter(db,  User)
    user_manager = UserManager(db_adapter, app)


    @app.route('/')
    def home_page():
        return render_template_string(....)


    return app

# Start development web server
if __name__=='__main__':
    app = create_app()
    app.run(host='0.0.0.0', port=8080, debug=True)

1 个答案:

答案 0 :(得分:1)

Heroku不会在底部运行此代码:

# Start development web server
if __name__=='__main__':
    app = create_app()
    app.run(host='0.0.0.0', port=8080, debug=True)

它运行您在procfile中提供的代码。您应该在procfile中执行create_app方法,如下所示。您将需要转义括号。

web: gunicorn -b 0.0.0.0:$PORT app:create_app\(\)

另外,仅供参考:您可以通过安装Heroku Toolbelt并在应用程序目录中运行heroku local来测试heroku的功能。