[英]Python Flask Frozen has no attribute 'url_default_functions'
I have written my 1st Python API project with Flask and now am trying to deploy it to Netlify.我已经用 Flask 编写了我的第一个 Python API 项目,现在正尝试将其部署到 Netlify。
Searched online and found I need to use Flask-Frozen to generate a static website.网上搜索发现需要使用Flask-Frozen生成一个static网站。 Not sure I'm doing it correctly, because my project is a API project not a website project, so may be I should not use Flask-Frozen(FF)?
不确定我做得对,因为我的项目是 API 项目而不是网站项目,所以我可能不应该使用 Flask-Frozen(FF)?
But if I could still use FF to generate static website for my API project, here is my project:但是如果我仍然可以使用 FF 为我的 API 项目生成 static 网站,这是我的项目:
-- --
Here is the app.py
这是
app.py
from flask_frozen import Freezer
from mazesolver import mazeapi
# Call the application factory function to construct a Flask application
# instance using the development configuration
# app = mazeapi()
# Create an instance of Freezer for generating the static files from
# the Flask application routes ('/', '/breakfast', etc.)
freezer = Freezer(mazeapi)
if __name__ == '__mazeapi__':
# Run the development server that generates the static files
# using Frozen-Flask
freezer.run(debug=True)
mazeapi.py
import io
from mazesolver.solver import MazeSolver
from markupsafe import escape
from flask import Flask, flash, request, redirect, send_file
from werkzeug.utils import secure_filename
ALLOWED_EXTENSIONS = { 'png', 'jpg', 'jpeg' }
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024
@app.route('/maze/<mazename>')
def maze(mazename):
return 'maze 4 %s' % escape(mazename)
Whenever I run: python app.py
每当我运行:
python app.py
I got this error:我收到了这个错误:
Traceback (most recent call last):
File "app.py", line 10, in <module>
freezer = Freezer(mazeapi)
File "/home/winstonfan/anaconda3/envs/maze/lib/python3.7/site-packages/flask_frozen/__init__.py", line 98, in __init__
self.init_app(app)
File "/home/winstonfan/anaconda3/envs/maze/lib/python3.7/site-packages/flask_frozen/__init__.py", line 108, in init_app
self.url_for_logger = UrlForLogger(app)
File "/home/winstonfan/anaconda3/envs/maze/lib/python3.7/site-packages/flask_frozen/__init__.py", line 588, in __init__
self.app.url_default_functions.setdefault(None, []).insert(0, logger)
AttributeError: module 'mazesolver.mazeapi' has no attribute 'url_default_functions'
I had the same issue and added我有同样的问题并添加
url_default_functions={}
to the app, right before到应用程序,就在之前
if __name__ == "__main__":
app.run()
I guess you could put it right after我想你可以把它放在后面
app = Flask(__name__)
and the error went way... but there are many more:) It seems that frozen-flask isn't working with the lastest flask, but I couldn't get any other versions working with it.并且错误发生了……但还有更多:) 似乎冷冻烧瓶不适用于最新的 flask,但我无法使用任何其他版本。
Did you manage to get it working?你设法让它工作吗?
You have a simple namespace bug.你有一个简单的命名空间错误。
In the code you've posted, you are freezing your module mazeapi
, not the actual Flask application, mazeapi.app
.在您发布的代码中,您正在冻结您的模块
mazeapi
,而不是实际的 Flask 应用程序mazeapi.app
。
Try changing your Freezer
call in line 10 of app.py
to:尝试将
app.py
第 10 行中的Freezer
调用更改为:
freezer = Freezer(mazeapi.app)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.