[英]Why do I keep getting the 405 error (Method Not Allowed The method is not allowed for the requested URL.) when the POST method is correctly set up?
I am learning Flask and I've incurred into a supposedly beginners error:我正在学习 Flask 并且我遇到了一个所谓的初学者错误:
Method Not Allowed The method is not allowed for the requested URL.
Method Not Allowed 请求的 URL 不允许使用该方法。
The thing is that, even though I reduce the code into something really simple, I can't find it.问题是,即使我将代码简化为非常简单的东西,我也找不到它。 As far as I can see, the POST method is correctly set up (I will use comments to explain it).
据我所见,POST 方法设置正确(我将使用注释来解释它)。
The files are:这些文件是:
flaskblog.py烧瓶博客.py
from flask import Flask, render_template, url_for, flash, redirect, request
from forms import RegistrationForm
app = Flask(__name__)
app.config['SECRET_KEY'] = '5791628bb0b13ce0c676dfde280ba245'
@app.route("/")
@app.route('/home', methods=['GET', 'POST']) # As one can see, the POST method is written there.
def home():
form = RegistrationForm()
return render_template('home.html', title='Home', form=form)
if __name__ == '__main__':
app.run(debug=True)
home.html主页.html
{% extends "layout.html" %}
{% block content %}
<div class="content-section">
<form method="POST" action=""> # The POST method is written there as well.
{{ form.hidden_tag() }}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Join Today</legend>
<div class="form-group">
{{ form.username.label(class="form-control-label") }}
{{ form.username(class="form-control form-control-lg") }}
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
</div>
</form>
</div>
{% endblock content %}
forms.py forms.py
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo
class RegistrationForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(min=2, max=20)])
submit = SubmitField('Sign Up')
I get the error when I submit the form, any help would be greatly appreciated!我在提交表单时收到错误,任何帮助将不胜感激!
Thanks!谢谢!
You're not distinguishing between GET
and POST
requests appropriately in your home
route.您没有在
home
路由中正确区分GET
和POST
请求。 Flask defaults the incoming request, from the client, as a GET
request since your definitions are off. Flask默认来自客户端的传入请求为
GET
请求,因为您的定义已关闭。 However, this is an issue because the incoming request is actually a POST
request as we can see from your form:但是,这是一个问题,因为传入的请求实际上是一个
POST
请求,我们可以从您的表单中看到:
<form method="POST" action=""> # The POST method is written there as well.
Perhaps you could try modifying your home
route to handle both POST
/ GET
requests approriately, something along the lines of:也许您可以尝试修改您的
home
路由以适当地处理POST
/ GET
请求,类似于:
@app.route("/")
@app.route('/home', methods=['GET', 'POST'])
def home():
form = RegistrationForm()
# POST request
if form.validate_on_submit():
return redirect(url_for(<route>)) #route to be rendered if form properly validates
# GET request
return render_template('home.html', title='Home', form=form)
Hopefully that helps!希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.