我正在嘗試運行我的第一個 Flask 應用程式,但是在 Postman 上發送 POST 請求后出現以下錯誤。
錯誤:werkzeug.exceptions.BadRequestKeyError:400 錯誤請求:瀏覽器(或代理)發送了此服務器無法理解的請求。關鍵錯誤:'目標'
錯誤可能來自這里:goal = request.form['goal'] ,我已經嘗試將 更改request.form為request.json或form.get沒有成功。:(
有任何想法嗎?
注意:我使用的是 Windows、Visual Studio、Python 3.9 和 Postman。
app = Flask(__name__)
@app.route('/create', methods=('GET', 'POST'))
def create():
if request.method == 'POST':
goal = request.form['goal']
content = request.form.get['content']
if not title:
flash('Goal is required!')
return calc(goal,content)
if __name__ == "__main__":
app.run(debug=True)
def calc(goal, content):
#rest of the code here...#
return jsonify({
"status": 200,
"message": "Success",
"data":data
})```
uj5u.com熱心網友回復:
在我看來,您尚未創建包含表單元素的初始基本網頁 (index.html)。
如果您查看https://github.com/jmcp/find-my-electorate/blob/master/app.py#L271,您會看到基本路由呼叫render_template()了一個引數,即https://github .com/jmcp/find-my-electorate/blob/master/templates/index.html。這將創建一個傳遞給 POST 方法的元素。
在您的代碼段中,您沒有類似的內容,因此根本沒有要傳遞的請求物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/403770.html
標籤:
上一篇:如何從API回應flask_restful回傳檔案?[復制]
下一篇:sqlalchemy.exc.ProgrammingErrorFlaskSQLAlchemy無法從我的表中讀取資料
