【问题标题】:Python Web App Using Flask, "app not defined" on Heroku使用 Flask 的 Python Web 应用程序,Heroku 上的“应用程序未定义”
【发布时间】:2016-01-19 02:56:53
【问题描述】:

我正在尝试在 heroku 上运行一个简单的应用程序,但是由于某些我不完全理解的原因,当我推送并尝试在 heroku 上运行时,下面的代码没有定义应用程序。当我在本地运行此代码时,使用python myApp.py 可以正常工作。我得到“应用程序未定义”。看来我需要在 create app 方法之外声明 app = Flask(__name__) 。我从烧瓶用户文档中获得了此代码。我怎样才能让它工作,以便我可以在 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)

【问题讨论】:

    标签: python heroku flask flask-login


    【解决方案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 的功能。

    【讨论】:

    • 谢谢。我很感激你的回答。这就说得通了。我有“main:app”运行 procfile 并没有意识到这一点。
    猜你喜欢
    • 2020-07-15
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 2019-05-20
    • 2015-09-25
    • 2015-01-05
    • 1970-01-01
    相关资源
    最近更新 更多