[英]ERR_TOO_MANY_REDIRECTS in a Flask application. Works in local but not in server
在本地,我的 Flask 應用程序工作正常,當我使用/editing_buddy
時,它會正確重定向我。 問題是,當我將它上傳到服務器時,它總是給我404 Error
。
如果我嘗試使用@app.route
裝飾器,我會收到TOO MANY REDIRECTS error
。 如果有幫助,我的服務器由 funpedia.toolforge.org 中的 Wikimedia 托管。 這是一個帶有 kubernetes 后端的網絡服務。
我的代碼是這樣的:
app = flask.Flask(__name__)
if __name__ == '__main__':
app.run_server(host='0.0.0.0', threaded=True, debug=True)
# Load configuration from YAML file
__dir__ = os.path.dirname(__file__)
app.config.update(
yaml.safe_load(open(os.path.join(__dir__, 'config.yaml'))))
@app.route('/')
def index():
return flask.redirect(flask.url_for('index'))
@app.route('/editing_buddy')
def buddy():
return flask.redirect(flask.url_for('buddy'))
@app.errorhandler(404)
def handling_page_not_found(e):
return "<h1>404</h1><p>The resource could not be found.</p>", 404
“/”和“/editing_buddy”的應用程序是具有相同 url_base_pathname 的 Dash 應用程序:
伙伴
buddy_app = Dash(__name__, server=application, url_base_pathname="/editing_buddy/",
external_stylesheets=external_stylesheets)
buddy_app.config['suppress_callback_exceptions'] = True
家
home_app = Dash(__name__, server=application, url_base_pathname="/",
external_stylesheets=external_stylesheets)
home_app.config['suppress_callback_exceptions'] = True
from view.home import *
from view.editing_buddy_app import *
在本地,我通過運行wsgi.py
來啟動服務器。 其中包含代碼:
from app import app as application
if __name__ == "__main__":
application.run()
在服務器中,我只是運行webservice restart
,但我有一個名為app.ini
的.ini 文件,其中包含以下內容:
[uwsgi]
module = wsgi
callable = application
PS :我也嘗試過硬編碼像flask.redirect("/editing_buddy/")
這樣的重定向。 即使我嘗試對 URL 像flask.redirect("funpedia.toolforge.org/editing_buddy/")
進行硬編碼,但要么找不到它,要么重定向太多。
你可以在這里看到我的回購
您實際上是在您的方法中重定向到您正在執行重定向的完全相同的方法。 通過這樣做,您正在創建一個無限循環,並且 TBH 這甚至應該在本地引發錯誤。
我建議您閱讀此medium.com 文章,以確保您使用應用程序工廠模式正確設置 Dash 和 Flask。
==================================================== =
編輯:
這是我鏈接的文章的復制/粘貼,您可以看到當 Flask 應用程序被實例化時,它還會創建 Dash 應用程序,使用 Flask 應用程序作為服務器...
儀表板.py
def create_app():
server = Flask(__name__)
server.config.from_object(BaseConfig)
register_dashapps(server)
# snipped some here
return server
def register_dashapps(app):
from app.dashapp1.layout import layout
from app.dashapp1.callbacks import register_callbacks
# Meta tags for viewport responsiveness
meta_viewport = {"name": "viewport", "content": "width=device-width, initial-scale=1, shrink-to-fit=no"}
dashapp1 = dash.Dash(__name__,
server=app,
url_base_pathname='/dashboard/',
assets_folder=get_root_path(__name__) + '/dashboard/assets/',
meta_tags=[meta_viewport])
with app.app_context():
dashapp1.title = 'Dashapp 1'
dashapp1.layout = layout
register_callbacks(dashapp1)
_protect_dashviews(dashapp1)
==================================================== =
編輯2:
感謝您添加回購,這有助於弄清楚發生了什么,我想我根據代碼的 rest 了解了您想要做什么。 我猜你想在你的應用程序中加入身份驗證邏輯,這樣不是每個人都可以訪問相應 Dash 應用程序的處理程序。
問題是,您遇到了與我鏈接的第一篇文章中描述的完全相同的問題,即身份驗證。
如果要使用身份驗證,則需要重構代碼,類似於上述文章中討論的內容。
在研究這個問題時,我確實遇到了一篇您可能感興趣的文章,因為它討論了如何實現身份驗證。 這也是一篇更簡單的文章,它基本上允許您將 Flask 的東西應用於 Dash 應用程序。 在這里找到它...
現在可以了,為什么? 我沒有任何線索。 我做了下一個:
app.ini
的目錄中去掉了app.py
,放到根目錄下。home.py
中,我from app import app as application
重命名為 just from app import app
。@app.route
,因為您已經有了url_base_pathname
,所以不需要它們。 經過一些測試,我發現解決方案是第2步。通過在home.py
腳本中使用from app import app
。
但是,在editor_buddy_app.py中,我將它與from app import app as application
editing_buddy_app.py
使用,並且它可以工作
當然,我在這里遺漏了一些東西,但我無法猜測是什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.