Python 方法不允许错误405
我正在开发烧瓶登记表,收到一个错误:Python 方法不允许错误405,python,forms,flask,Python,Forms,Flask,我正在开发烧瓶登记表,收到一个错误: error 405 method not found. 代码: registration.html: <html> <head> <title>Form di registrazione </title> </head> <body> {{ username }} <form id='registration' action='/registrazi
error 405 method not found.
代码:
registration.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>
<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>
注册表格
{{username}}
尤内特酒店
诺姆:
Indirizzo邮件:
用户名*:
密码*:
当我访问
localhost:5000/registrazione
时,我收到了错误。我做错了什么?这是因为定义路由时只允许POST请求
当您在浏览器中访问/registrazione
时,它将首先执行GET请求。只有提交表单后,浏览器才会发布帖子。因此,对于像您这样的自提交表单,您需要同时处理这两个问题
使用
@app.route('/registrazione', methods=['GET', 'POST'])
应该可以工作。使用wsgi和JQuery、Ajax和json的flask应用程序示例:
<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>
<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>
activecalls.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\u calls\u map.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>
<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>
$.ajax({
//url:“http://dev.consumerunited.com/wsgi/activecalls.py/_getData",
url:“activecalls.py/_getData”,
类型:“POST”,
数据:formData,
数据类型:“jsonp”,
成功:函数(数据、文本状态、jqXHR)
{
//数据-来自服务器的响应
警报(“'”+data.response.blah+“”);
},
错误:函数(jqXHR、textStatus、errorshown)
{
警报(“错误:+错误抛出”);
}
});
当你运行这个。警告框会打印:“东西”。只供现在阅读的人使用。 在访问表单数据之前,必须先呈现/registrazione。就写吧
@app.route("/registrazione")
def render_registrazione() -> "html":
return render_template("registrazione.html")
在定义def注册()之前。顺序是关键。在偶数可用之前,您无法访问数据。这是我对问题的理解。更改方法注册的名称
@app.route('/registrazione', methods=['POST'])
def registrazione():
if request.method == 'POST':
username= request.form.username.data
return render_template('registration.html', username=username)
else :
return render_template('registration.html')
methods=['POST']
与if request.method=='POST':(…)else:
so非常不兼容。您收到一个method no allowed error,并且您正在对一个声明为仅接受POST
的路由执行GET
请求。你现在明白为什么了吗?是的,我添加了@app.route('/registrazione',methods=['GET',POST']),但是现在我收到了错误500内部服务器错误,可能是因为{{username}}
没有定义,但是你有日志,所以你应该知道。我认为用户名。
数据是错误的。只要使用username=request.form.username
就可以了。谢谢,现在可以了。但如果我编辑def registration():if request.method='POST':username=request.form.username.data返回渲染_模板('registration.html',username=username)否则:返回渲染_模板('registration.html')我收到内部服务器Error@Matteo这是另一个问题,请创建一个新问题并包含堆栈跟踪。因为我遇到了相同的问题,或者至少它看起来与我相似,因为我收到了相同的错误消息,并且为我提供的解决方案不起作用。但是,如果我先在项目中呈现带有表单的模板,然后尝试使用新函数和post方法访问表单数据,那么一切都很好。所以我想我不能发布一些不存在的东西。如果另一个不适合我,我为什么不发布我的解决方案?