Python Flask 错误:“方法不允许 请求的 URL 不允许该方法”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20689195/
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 21:02:21  来源:igfitidea点击:

Flask Error: "Method Not Allowed The method is not allowed for the requested URL"

pythonflask

提问by Suraj Kapoor

I'm getting the following error whenever I try to submit data to my Flask form:

每当我尝试向 Flask 表单提交数据时,都会收到以下错误:

Method Not Allowed The method is not allowed for the requested URL.

I think the issue is in the return redirect(url_for('database'))I'm doing. I've also tried return render_template('database.html)too. I'm trying to call the database page once form entries have been submitted to the database.

我认为问题出在return redirect(url_for('database'))我正在做的事情上。我也试过return render_template('database.html)了。一旦表单条目提交到数据库,我试图调用数据库页面。

Relevant parts of my code are as follows:

我的代码的相关部分如下:

@app.route('/entry', methods=['GET', 'POST'])
def entry_page():
    if request.method == 'POST':
        date = request.form['date']
        title = request.form['blog_title']
        post = request.form['blog_main']
        post_entry = models.BlogPost(date = date, title = title, post = post)
        db.session.add(post_entry)
        db.session.commit()
        return redirect(url_for('database'))
    else:
        return render_template('entry.html')

@app.route('/database')        
def database():
    query = []
    for i in session.query(models.BlogPost):
        query.append((i.title, i.post, i.date))
    return render_template('database.html', query = query)

entry.html is...

entry.html 是...

THIS IS THE BLOG ENTRY PAGE

blog:
<html>
    <form action='/database' method = "post">
        date<input name = "date" type = "text" class="text">
        title<input name = "blog_title" type = "text" class="text">
        main<input name = "blog_main" type = "text" class="text">
        <input type = "submit">
    </form> 
</html>

and database.html...

和数据库.html ...

THIS IS THE QUERY:

{{query}}

采纳答案by Wondercricket

What is happening here is that database route does not accept any url methods.

这里发生的事情是数据库路由不接受任何 url 方法。

I would try putting the url methods in the app route just like you have in the entry_page function:

我会尝试将 url 方法放在应用程序路由中,就像在 entry_page 函数中一样:

@app.route('/entry', methods=['GET', 'POST'])
def entry_page():
    if request.method == 'POST':
        date = request.form['date']
        title = request.form['blog_title']
        post = request.form['blog_main']
        post_entry = models.BlogPost(date = date, title = title, post = post)
        db.session.add(post_entry)
        db.session.commit()
        return redirect(url_for('database'))
    else:
        return render_template('entry.html')

@app.route('/database', methods=['GET', 'POST'])        
def database():
    query = []
    for i in session.query(models.BlogPost):
        query.append((i.title, i.post, i.date))
    return render_template('database.html', query = query)

回答by Hossein

I had a similar problem when I deployed my Flask app in the IIS. Apparently, IIS does not accept route that include an underline ("_"). When I removed the underline, problem was resolved.

当我在 IIS 中部署 Flask 应用程序时,我遇到了类似的问题。显然,IIS 不接受包含下划线(“_”)的路由。当我删除下划线时,问题就解决了。

回答by oneGiantSmiley

I also had similar problem where redirects were giving 404 or 405 randomly on my development server. It was an issue with gunicorn instances.

我也有类似的问题,重定向在我的开发服务器上随机给出 404 或 405。这是 gunicorn 实例的问题。

Turns out that I had not properly shut down the gunicorn instance before starting a new one for testing. Somehow both of the processes were running simultaneously, listening to the same port 8080 and interfering with each other. Strangely enough they continued running in background after I had killed all my terminals. Had to kill them manually using fuser -k 8080/tcp

事实证明,在开始一个新的测试之前,我没有正确关闭 gunicorn 实例。不知何故,这两个进程同时运行,监听同一个端口 8080 并相互干扰。奇怪的是,在我杀死所有终端后,它们继续在后台运行。不得不使用手动杀死它们fuser -k 8080/tcp

回答by Valerian Ardelean

I had the same problem, and my solving was to replace :

我遇到了同样的问题,我的解决方法是替换:

return redirect(url_for('index'))

with

return render_template('indexo.html',data=Todos.query.all())

in my POSTand DELETEroute.

在我POSTDELETE路线。