简体   繁体   English

为什么正确设置 POST 方法后,我不断收到 405 错误(Method Not Allowed The method is not allowed for the requested URL.)?

[英]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路由中正确区分GETPOST请求。 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.

相关问题 Method Not Allowed 请求的 URL 不允许使用该方法。 405 错误 - Method Not Allowed The method is not allowed for the requested URL. 405 Error 405 Method Not Allowed:请求的 URL 不允许使用该方法 - 405 Method Not Allowed: The method is not allowed for the requested URL 不允许的方法 请求的 URL 不允许使用该方法。 在 DELETE 方法烧瓶上 - Method Not Allowed The method is not allowed for the requested URL. on DELETE Method Flask 所请求的URL不允许使用该方法。 在烧瓶 - The method is not allowed for the requested URL. in Flask 为什么我在使用 urllib2 请求 URL 时收到“HTTP 错误 405:方法不允许”? - Why am I getting “HTTP Error 405: Method Not Allowed” when requesting a URL using urllib2? 尝试使用 Flask POST 表单数据时,不断收到 HTTP 405 Method Not Allowed 错误 - Keep getting HTTP 405 Method Not Allowed error when trying to POST form data with Flask 405:方法不允许。 为什么我在 Flask 中收到此错误 - 405: Method Not Allowed. Why am I getting this error in Flask 405 不允许发布方法 - 405 Method Post Not Allowed 如何修复 {&quot;message&quot;: &quot;请求的 URL 不允许使用该方法。&quot; 对于我的 post 方法? - How to fix {"message": "The method is not allowed for the requested URL." } for my post method? Python requsts.post返回405错误:请求的URL不允许使用该方法 - Python requsts.post returning 405 error: The method is not allowed for the requested URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM