'Method Not Allowed: The method is not allowed for the requested URL.' 提交文本输入时

'Method Not Allowed: The method is not allowed for the requested URL.' when submitting text entry

我是运行烧瓶python和HTML一样,问题很简单:

它给我错误 405:方法不允许:所请求的方法不允许 URL。我查了一下,有人说在页面路由中包含 methods=['GET', 'POST'],但我已经有了这个。

Python代码:

@app.route('/')
@app.route('/home/', methods=['GET', 'POST'])
def home():
    global count
    guess = request.form.get('guess')
    result, valid = guess_word(guess, word, words)
    print(result, valid)
    try:
            guesses[count-1][2] = 'p'
    except:
            guesses[count-1][2] = ''
    if count < 6:
        if valid:
            guesses[count][0] = guess
            guesses[count][1] = result
    session['guesses'] = guesses
    if valid:
        count += 1
    return render_template('index.html', guesses=session['guesses'])

HTML代码:

<div class="container2">
    <form method="post">
        <input type="text" placeholder="Guess" class="text-input" name="guess">
    </form>
</div>

这以前有效,我没有改变(我认为是)什么,但它突然停止工作。当我提交文本条目时它给我错误。

您的表单没有 action 属性,即您没有明确说明提交表单时数据应该去哪里。在没有 action 属性的情况下,它将假设您的主页是 / 并且从您的路由来看,这不支持 POST.

2 种可能的解决方案

  1. action 属性添加到您的表单中
<form action ="home" method ="post">
  1. 保持您的数据不变,并为 / 的路线添加对 POST 的支持,即将您的路线代码更改为
@app.route('/', methods=['GET', 'POST'])
@app.route('/home/', methods=['GET', 'POST'])