简体   繁体   English

Python Flask Frozen 没有属性 'url_default_functions'

[英]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 网站,这是我的项目:

-- --

  • app.py应用程序.py
  • mazesolver迷宫解算器
    • mazeapi.py mazeapi.py

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.

相关问题 冷冻瓶中URL生成器的问题 - Problems with URL generator in Frozen-flask Python flask - object 没有属性错误 - Python flask - object has no attribute error Python Flask 如果 url 中有 /#/,则重定向不起作用 - Python Flask redirection not working if the url has /#/ in it Flask:ImmutableMultiDict没有属性 - Flask : ImmutableMultiDict has no attribute Python - AttributeError: 'NoneType' object 没有属性 'cursor' python flask - Python - AttributeError: 'NoneType' object has no attribute 'cursor' python flask 从 Python Flask 中的变量指定模板名称(使用 Flask Frozen) - Specify template name from variable in Python Flask (using Flask Frozen) Python 数据类:Mocking 冻结数据类中的默认工厂 - Python Dataclasses: Mocking the default factory in a frozen Dataclass Python Flask:AttributeError:&#39;NoneType&#39;对象没有属性&#39;is_active&#39; - Python Flask: AttributeError: 'NoneType' object has no attribute 'is_active' MongoDB - Python Flask 问题:AttributeError: &#39;Cursor&#39; 对象没有属性 &#39;title&#39; - MongoDB - Python Flask problem: AttributeError: 'Cursor' object has no attribute 'title' PYTHON-Flask:“用户”对象没有属性“ is_active” - PYTHON - Flask : 'User' object has no attribute 'is_active'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM