我是构建 API 的新手。我正在构建一个非常简单的 API:执行时,显示 API 的 HTML 页面将要求输入名称:Api 将返回:“Hello {name}”。我收到 405 错误。这是我的 App.py 和 HTML 页面代码:
from app import app
from flask import render_template
from flask import request
@app.route('/')
def home():
return "hello no world!"
@app.route('/template',methods=['POST','GET'])
def template():
output = [request.form.values()]
output = output[0]
return render_template('home.html',prediction_text='Hello {}'.format(output))
和我的 home.html 的 HTML 代码:
!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Welcome home</title>
</head>
<body>
<div class="login">
<h1>Enter your name</h1>
<!-- Main Input For Receiving Query-->
<form action="{{ url_for('template')}}"method="post">
<input type="text" name="name" placeholder="Name" required="required" />
<button type="submit" class="btn btn-primary btn-block btn-large">Enter</button>
</form>
<br>
<br>
{{ prediction_text }}
</div>
</body>
</html>
我曾就这个问题查看过其他几个 StackOverflow 论坛。 “GET”或“POST”方法似乎有问题,但我似乎无法弄清楚是什么?也许你们中的一个人可以看到我没有看到的东西。我在 Docker 内部运行这个 API,所以“app = Flask(name)”存储在别处,如果相关的话。
Method Not Allowed - The method is not allowed for the requested URL
Flask - POST - The method is not allowed for the requested URL
https://www.reddit.com/r/flask/comments/a04aew/ask_flask_help_me_to_find_the_error_in_my_code/
答案 0 :(得分:0)
这个问题现在已经解决了。我改变了两件事:
来自 HTML: 改变:
<form action="{{ url_for('template')}}"method="post">
到
<form action="{{ url_for('template')}}"method="get">
并从 Flask API 更改:
output = [request.form.values()]
output = output[0]
return render_template('home.html',prediction_text='Hello {}'.format(output))
到
output = request.args.values()
return render_template('home.html',prediction_text="Hello {}?".format(list(output)[:]))