Python 方法不允许。我收到的是GET而不是POST

Python 方法不允许。我收到的是GET而不是POST,python,post,flask,Python,Post,Flask,我正在尝试适应FlaskAPI,我正在创建一个简单的页面,其中包含一个表单,使用POST方法 相反,当我查看Flask输出时,我可以看到,每当我尝试打开/configure视图时,Flask都会收到一个GET方法,并返回视图上不允许的方法 test.py- from flask import Flask, request, session, g, redirect, url_for, \ abort, render_template, flash import dude app = F

我正在尝试适应FlaskAPI,我正在创建一个简单的页面,其中包含一个表单,使用POST方法

相反,当我查看Flask输出时,我可以看到,每当我尝试打开/configure视图时,Flask都会收到一个GET方法,并返回视图上不允许的方法

test.py-

from flask import Flask, request, session, g, redirect, url_for, \
    abort, render_template, flash
import dude

app = Flask(__name__)


@app.route('/')
def hello():
    output = None
    if retval:
        output = "True"
    elif retval == False:
        output = "False" + error
    return render_template('index.html', output=output)

@app.route('/configure', methods=['POST'])
def configure():
    hostname = request.form['hostname']
    if hostname in ip_dict:
        _info.append(hostname)
        flash('good job')
    else:
        flash('Host name not in the DNS system')
    return render_template('configure.html')

if __name__== "__main__":
    WB = dude.Dude()
    retval, error, ip_dict = WB.get_ips()
    app.run(debug=True)
index.html

<!doctype html>
<title>configurator</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<div class=page>
    <h1>Configurator</h1>
    <div class=metanav>
        <p>{{ output }}</p>
        <a href="{{ url_for('configure') }}">Configure</a>
    </div>
    {% for message in get_flashed_messages() %}
        <div class=flash>{{ message }}</div>
    {% endfor %}
    {% block body %}{% endblock %}
</div>
更新

我发现了我的错误。我在一个地方呈现template index.html,我应该在这里呈现configure.html。在此之后,在configure()中,我将呈现一个模板,我应该在其中重定向

这就是新方法的样子

@app.route('/')
def hello():
    output = None
    if retval:
        output = "True"
    elif retval == False:
        output = "False" + error
    return render_template('configure.html', output=output)

@app.route('/comeon', methods=['POST'])
def configure():
    hostname = request.form['hostname']
    if hostname in ip_dict:
        _info.append(hostname)
        flash('good job')
    else:
        flash('Host name not in the DNS system')
    return redirect(url_for('hello'))

然后您尝试在浏览器中打开
/configure
,默认情况下使用
GET
方法。 由于您只为该url指定了
POST
,因此出现了错误

你的表格有一些问题

render_template('configure.html')
您应该传递一些参数,例如
hostname
,因为您试图在模板
中使用它

这是不正确的语法。它应该是:

<dd><input type=text name={{ hostname }} size=15>


等等。

嗨,谢谢你的回复。不幸的是,我没有尝试将变量从Python传递到模板。我试图在输入表单中编写一个主机名,将其传递给python并计算主机名是否在DNS中。无论如何,我试图应用您建议的更改,但错误没有改变。@VictorDaskalov(s)他说您将无法在浏览器中打开“/configure”视图。您将只能发布到该页面(即使用主页上的表单)。如果您希望能够导航到“/configure”页面,请将“GET”添加到
方法
。您可以从修复HTML开始。属性值周围必须有引号。谢谢您的评论,但这并不能解决我的问题。@KlausD。那完全不是真的。
render_template('configure.html')
<dd><input type=text name={{ hostname }} size=15>