方法不允许烧瓶错误405
- 2025-03-20 08:48:00
- admin 原创
- 37
问题描述:
我正在开发一个 Flask 注册表,然后收到一个错误:
error 405 method not found.
代码:
import os
# Flask
from flask import Flask, request, session, g, redirect, url_for, abort, \n 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')
注册.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>
当我访问 时localhost:5000/registrazione
,我收到错误。我做错了什么?
解决方案 1:
这是因为您在定义路线时只允许 POST 请求。
当您在浏览器中访问时/registrazione
,它会首先发出 GET 请求。只有当您提交表单后,浏览器才会发出 POST。因此,对于像您这样的自提交表单,您需要同时处理这两者。
使用
@app.route('/registrazione', methods=['GET', 'POST'])
应该可以。
解决方案 2:
更改方法注册的名称
@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')
解决方案 3:
仅供现在正在阅读的人参考。您必须先呈现 /registrazione,然后才能访问表单数据。只需写入即可。
@app.route("/registrazione")
def render_registrazione() -> "html":
return render_template("registrazione.html")
在定义 def registration() 之前。顺序是关键。在事件可用之前,您无法访问数据。这是我对这个问题的理解。
解决方案 4:
对于错误 500(内部服务器错误)
username = request.form.username
改写
username = request.args.get("username")
解决方案 5:
使用 wsgi 与 JQuery、Ajax 和 json 的 Flask 应用程序示例:
主动调用.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:
<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>
当你运行这个时,警告框会打印:“stuff”。
解决方案 6:
我也遇到了这个错误,我查看了所有文档并试图解决这个问题,但最终这是一个愚蠢的错误。
下面的代码产生了405 Method Not Allowed
错误
import requests
import json
URL = "http://hostname.com.sa/fetchdata/"
PARAMS = '{ "id":"111", "age":30, "city":"New Heaven"}'
response = requests.post(url = URL, json = PARAMS)
print(response.content)
这是因为/
URL 末尾有一个多余的内容,当我删除它时,它就消失了。下面对请求 URL 的更新修复了这个问题
URL = "http://hostname.com.sa/fetchdata"
解决方案 7:
url路径和处理方法命名要一致。
要解决此问题,请将 registration() 函数名称更改为 registrazione()
@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')
解决方案 8:
我被同样的问题困扰了,我将登录页面路由显示为默认路由,当我尝试使用默认路由提交时,我遇到了这个问题,因为我已经在登录路由上配置了 POST 请求,但没有在默认应用程序路由上配置,当我为我的默认路由添加了“POST”方法配置时,一切都按预期工作。我所做的配置如下:
@routes.route("/", methods=['GET', 'POST'] )
@routes.route("/admin-login", methods=['GET', 'POST'])
def admin_login():
...
希望这可以帮助任何面临类似问题的人。
扫码咨询,免费领取项目管理大礼包!