Python 方法不允许烧瓶错误 405

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21689364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:24:03  来源:igfitidea点击:

Method Not Allowed flask error 405

pythonformsflask

提问by Matteo

I am developing a flask registration form, and I receive an error:

我正在开发一个烧瓶注册表,我收到一个错误:

error 405 method not found.

Code:

代码:

import os
# Flask
from flask import Flask, request, session, g, redirect, url_for, abort, \
     render_template, flash, Markup, send_from_directory, escape
from werkzeug import secure_filename

from cultura import app

# My app
from include import User

@app.route('/')
def index():
    return render_template('hello.html')

@app.route('/registrazione', methods=['POST']) 
def registration():
    if request.method == 'POST':
        username= request.form.username.data
        return render_template('registration.html', username=username)
    else :
        return render_template('registration.html')

registration.html:

注册.html:

<html>
<head> <title>Form di registrazione </title>
 </head> 

    <body>
    {{ username }}
    <form id='registration' action='/registrazione' method='post'>
    <fieldset >
    <legend>Registrazione utente</legend>
    <input type='hidden' name='submitted' id='submitted' value='1'/>
    <label for='name' >Nome: </label> 
    <input type='text' name='name' id='name' maxlength="50" /> <br>
    <label for='email' >Indirizzo mail:</label>
    <input type='text' name='email' id='email' maxlength="50" />
     <br>
    <label for='username' >UserName*:</label>
    <input type='text' name='username' id='username' maxlength="50" />
     <br>
    <label for='password' >Password*:</label>
    <input type='password' name='password' id='password' maxlength="50" />
    <br>
    <input type='submit' name='Submit' value='Submit' />

    </fieldset>
    </form>
    </body>
    </html>

when I visit localhost:5000/registrazione, I receive the error. What am I doing wrong?

当我访问时localhost:5000/registrazione,我收到错误。我究竟做错了什么?

回答by Lukas Graf

This is because you only allow POST requests when defining your route.

这是因为您在定义路由时只允许 POST 请求。

When you visit /registrazionein your browser, it will do a GET request first. Only once you submit the form your browser will do a POST. So for a self-submitting form like yours, you need to handle both.

当您/registrazione在浏览器中访问时,它会首先执行 GET 请求。只有在您提交表单后,您的浏览器才会执行 POST。因此,对于像您这样的自行提交表单,您需要同时处理两者。

Using

使用

@app.route('/registrazione', methods=['GET', 'POST']) 

should work.

应该管用。

回答by Eric Leschinski

Example of a flask app using wsgi with JQuery, Ajax and json:

使用 wsgi 和 JQuery、Ajax 和 json 的 Flask 应用程序示例:

activecalls.py

主动调用.py

from flask import Flask, jsonify

application = Flask(__name__, static_url_path='')

@application.route('/')
def activecalls():
    return application.send_static_file('activecalls/active_calls_map.html')

@application.route('/_getData', methods=['GET', 'POST'])
def getData():
    #hit the data, package it, put it into json.
    #ajax would have to hit this every so often to get latest data.
    arr = {}
    arr["blah"] = []
    arr["blah"].append("stuff");

    return jsonify(response=arr)


if __name__ == '__main__':
    application.run()

Javascript json, /static/activecalls/active_calls_map.html:

Javascript json,/static/activecalls/active_calls_map.html:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

<script>
$.ajax({
    //url : "http://dev.consumerunited.com/wsgi/activecalls.py/_getData",
    url : "activecalls.py/_getData",
    type: "POST",
    data : formData,
    datatype : "jsonp",
    success: function(data, textStatus, jqXHR)
    {
        //data - response from server
         alert("'" + data.response.blah + "'");
    },
    error: function (jqXHR, textStatus, errorThrown)
    {
         alert("error: " + errorThrown);

    }
});
</script>

When you run this. The alert box prints: "stuff".

当你运行这个。警报框打印:“东西”。

回答by nullbyte

Just for people reading on it now. You have to render the /registrazione first, befor you can access the form data. Just write.

仅供现在阅读它的人使用。您必须先渲染 /registrazione,然后才能访问表单数据。写就好了。

@app.route("/registrazione")
    def render_registrazione() -> "html":
        return render_template("registrazione.html")

before you define def registration(). Sequence is key. You can't access data before the even are available. This is my understanding of the problem.

在定义 def registration() 之前。顺序是关键。在偶数可用之前,您无法访问数据。这是我对问题的理解。