Python 方法不允许该方法不允许用于请求的URL。405错误
我不熟悉建筑API。我正在构建一个非常简单的API:执行时,显示API的HTML页面将要求输入名称:API将只返回:“Hello{name}”。我得到了405个错误。以下是我的App.py和HTML页面代码:Python 方法不允许该方法不允许用于请求的URL。405错误,python,html,api,flask,Python,Html,Api,Flask,我不熟悉建筑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!&quo
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>
!doctype html>
欢迎回家
输入您的姓名
进入
{{prediction_text}}
我已经看过了几个其他的StackOverflow论坛。“GET”或“POST”方法似乎有问题,但我似乎不知道是什么?也许你们中的一个可以看到一些我没有看到的东西。我在Docker中运行此API,因此“app=Flask(name)”存储在其他位置(如果相关的话)
这个问题现在已经解决了。我改变了两件事: 从HTML: 更改:
<form action="{{ url_for('template')}}"method="post">
到
超文本传输协议(HTTP)405方法,不允许响应状态代码表示服务器知道该请求方法,但目标资源不支持该方法。因此,您使用什么方法,POST?具体要求是什么?如何通过Postman,curl运行它?@Alveona由于我在Docker中运行该应用程序,所以我有单独的文件用于使用Ubuntu终端运行该程序。至于方法,我用来构建我的程序的视频专门使用“POST”方法,但我在某处读到,如果我们的输出要显示在HTML上,它需要同时具有“GET”和“POST”。一切正常,但当我输入一个名称并按enter键时,会显示405。更新:在我的HTML页面中:我将我的:从“post”更改为“get”,现在确实得到了输出。然而,它是这样的:“你好”,而不是我打开URL时输入的名称。有什么想法吗?您需要类似于
request.form['name']
的东西来获取文本框的值,因为您已经将其称为'name'。并且您的路由/模板接受POST请求,这就是为什么此路由/模板与表单一起工作,而路由/模板不工作的原因。
<form action="{{ url_for('template')}}"method="get">
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)[:]))