由于我无法解决持续存在的错误,在 AWS 上的 Ubuntu 上托管 Flask 应用程序时遇到了困难。 Gunicorn 日志中标识的错误特别指向 TypeError,其中“Flask”对象不可迭代。该问题似乎与 favicon.ico 文件请求的处理有关。
[2023-12-29 22:32:39 +0000] [5686] [ERROR] Error handling request /favicon.ico
Traceback (most recent call last):
File "/home/ubuntu/venv/lib/python3.10/site-packages/gunicorn/workers/sync.py", line 135, in handle
self.handle_request(listener, req, client, addr)
File "/home/ubuntu/venv/lib/python3.10/site-packages/gunicorn/workers/sync.py", line 183, in handle_request
for item in respiter:
TypeError: 'Flask' object is not iterable
main.py
from myAPP import create_app
from flask_mail import Mail
app = create_app()
@app.template_filter('custom_b64encode')
def custom_b64encode(data):
return base64.b64encode(data).decode('utf-8')
from flask import render_template
@app.errorhandler(404)
def not_found_error(error):
return render_template('404.html'), 404
from myAPP.views import views
app.register_blueprint(views, name='my_views')
app.config['UPLOAD_FOLDER'] = '/photos/'
if __name__ == '__main__':
app.run()
__init__.py
db = SQLAlchemy()
DB_NAME = "database.db"
mail = Mail() # Initialize mail
def create_app(environ=None, start_response=None):
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}'
mail = Mail(app)
app.config['SECRET_KEY'] = 'SecretKey'
app.config['SECURITY_PASSWORD_SALT'] = 'SecurityPasswordSalt'
app.config['MAIL_SERVER'] = 'example.home.pl'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True # Enable TLS
app.config['MAIL_USE_SSL'] = False # Keep SSL disabled
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME', 'username')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD', 'password')
app.config['MAIL_DEFAULT_SENDER'] = '[email protected]'
db.init_app(app)
mail.init_app(app)
with app.app_context():
db.create_all()
from .views import views
from .auth import auth
app.register_blueprint(views, url_prefix='/')
app.register_blueprint(auth, url_prefix='/')
from .models import User, Photo
login_manager = LoginManager()
login_manager.login_view = 'auth.loginPage'
login_manager.init_app(app)
@login_manager.user_loader
def load_user(id):
return User.query.get(int(id))
from . import models
with app.app_context():
db.create_all()
return app
app = create_app()
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
s = URLSafeTimedSerializer(app.config['SECRET_KEY'])
作为初学者,我尝试使用整个数据库来参与chatGPT,但遇到了问题并且无法达到预期的结果。
通常这个错误是由于将 Flask 应用程序对象以外的东西传递给 uwsgi (Gunicorn) 引起的。 uwsgi 的可调用设置是什么?如果您使用函数来生成应用程序,请确保调用该函数而不仅仅是传递函数对象。