代码之家  ›  专栏  ›  技术社区  ›  applecrusher

Python Web应用程序使用Flask,Heroku上的“应用程序未定义”

  •  0
  • applecrusher  · 技术社区  · 10 年前

    我正试图在heroku上运行一个简单的应用程序,但是由于我不完全理解的原因,当我推送并尝试在heroko上运行时,应用程序没有被下面的代码定义。当我在本地运行此代码时,使用 python myApp.py .I得到“未定义应用程序”。看来我需要申报 app = Flask(__name__) 在createapp方法之外。我从烧瓶用户文档中得到了这个代码。我如何让它工作,以便我可以在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 回复  |  直到 10 年前
        1
  •  1
  •   jumbopap    10 年前

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

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

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

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

    同样仅供参考:您可以通过安装heroku Toolbelt并运行 heroku local 在应用程序目录中。