成功部署我的 gcloud 應用程式后,我無法在 Web 瀏覽器中運行它。
當我嘗試使用 gcloud app browse 在瀏覽器中打開它時出現的錯誤是“502 Bad Gateway nginx”
這就是我在 app.py 檔案的頁面之間導航的方式:
@app.route("/", methods=['GET','POST'])
def index():
# If you're just opening your home page
if request.method == "GET":
return render_template('index.html')
# If you submitted one of the forms
elif request.method == "POST":
if request.values.get("action1", None) == 'Calculator':
return render_template('/densitycalc.html')
elif request.values.get("action2", None) == 'Linear Feet':
return render_template('/linearFeet.html')
elif request.values.get("action3", None) == 'Insurance':
return render_template('insurance.html')
elif request.values.get("action4", None) == 'Home':
return render_template('index.html')
在每個 HTML 頁面上,這是我導航回下面的索引頁面的方式:
<head>
<h1>"{name of page}"</h1>
<form method="post" action="/" class="fcMenuBar">
<input type="submit" name="{corresponding page name}" value="{corresponding page value}">
</form>
</head>
應用程式.yaml
runtime: python38
entrypoint: gunicorn -b :$PORT main:app
要求.txt
Flask==2.1.2
gunicorn==20.1.0
我是否在尋找正確的位置來識別錯誤?還是我缺少另一個檔案?
uj5u.com熱心網友回復:
您的代碼當前的撰寫方式給人的印象是屬性 - action1、action2、action3、action4 始終存在,但在您的 html 頁面上似乎并非如此。
將您的代碼更改為
# If you're just opening your home page
if request.method == "GET":
return render_template('index.html')
# If you submitted one of the forms
elif request.method == "POST":
if request.values.get("action", None) == 'Calculator':
return render_template('/densitycalc.html')
elif request.values.get("action2", None) == 'Linear Feet':
return render_template('/linearFeet.html')
elif request.values.get("action3", None) == 'Insurance':
return render_template('insurance.html')
elif request.values.get("action4", None) == 'Home':
return render_template('index.html')
通過使用request.values.get("action", None),您可以處理屬性“action”不存在的情況(如果該屬性不存在,則默認值為 None)
更新(因為您現在有不同的錯誤)
您的帖子談論app.py但您的 app.yaml 檔案有一個entrypointwhich uses main:app。
如果您實際上沒有main.py正在匯入app.py且正在其中app創建物件的檔案,那么您應該重命名app.py為main.py或更新入口點以參考app:app(我不建議使用后一個選項,因為您的檔案現在具有相同的名稱作為app物件)
dev_appserver.py如果您想知道為什么上述問題在 Dev 中不是問題,但在生產中是問題 - 如果您運行您的應用程式將模仿 Google 的生產服務器,則該錯誤只會在本地發生。如果您不在本地運行 with dev_appserver.py,那么您的本地環境沒有使用app.yaml這意味著它沒有使用您的entrypoint引數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/503692.html
標籤:Google Cloud Collective Python html 烧瓶 谷歌应用引擎 云
下一篇:Shell腳本命令有助于縮短?
