简体   繁体   English

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

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

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.我认为问题出在我正在做的return redirect(url_for('database'))中。 I've also tried return render_template('database.html) too.我也尝试过return render_template('database.html) I'm trying to call the database page once form entries have been submitted to the database.将表单条目提交到数据库后,我正在尝试调用数据库页面。

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...入口.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}}

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)

I think you forgot to add methods for your database function.我想你忘了为你的数据库函数添加方法。

@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)

I had a similar problem when I deployed my Flask app in the IIS.当我在 IIS 中部署我的 Flask 应用程序时,我遇到了类似的问题。 Apparently, IIS does not accept route that include an underline ("_").显然,IIS 不接受包含下划线(“_”)的路由。 When I removed the underline, problem was resolved.当我删除下划线时,问题就解决了。

I had the same problem, and my solving was to replace :我有同样的问题,我的解决办法是更换:

return redirect(url_for('index'))

with

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

in my POST and DELETE route.在我的POSTDELETE路线中。

I also had similar problem where redirects were giving 404 or 405 randomly on my development server.我也有类似的问题,重定向在我的开发服务器上随机给出 404 或 405。 It was an issue with gunicorn instances.这是 gunicorn 实例的问题。

Turns out that I had not properly shut down the gunicorn instance before starting a new one for testing.事实证明,在开始新的测试之前,我没有正确关闭 gunicorn 实例。 Somehow both of the processes were running simultaneously, listening to the same port 8080 and interfering with each other.不知何故,这两个进程同时运行,监听同一个端口 8080 并相互干扰。 Strangely enough they continued running in background after I had killed all my terminals.奇怪的是,在我杀死所有终端后,它们继续在后台运行。 Had to kill them manually using fuser -k 8080/tcp必须使用fuser -k 8080/tcp手动杀死它们

flask need to have enctype="" must be added in the form tag.烧瓶需要有 enctype="" 必须在表单标签中添加。 Like below.如下所示。

<html>
    <form action='/database' method = "post" enctype="multipart/form-data">
        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>

try to add methods尝试添加方法

@app.route('/database', methods=['GET', 'POST']) @app.route('/database', methods=['GET', 'POST'])

如果在不保存Python文件的情况下运行Web应用程序,有时会出现此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask 错误:“方法不允许 请求的 URL 不允许该方法” - Flask Error: “Method Not Allowed The method is not allowed for the requested URL” Flask 错误:方法不允许 请求的 URL 不允许该方法 - Flask error: Method Not Allowed The method is not allowed for the requested URL Flask 错误:方法不允许 请求的 URL 不允许该方法 - Flask error : Method Not Allowed The method is not allowed for the requested URL Python Flask:错误“请求的 URL 不允许使用该方法” - Python Flask: Error "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 Python、Flask:方法不允许,请求的方法不允许 URL - Python, Flask: Method Not Allowed, The method is not allowed for the requested URL Python flask flask不允许的方法所请求的URL不允许使用该方法 - Python flask Method Not Allowed The method is not allowed for the requested URL Flask - POST - 请求的URL不允许使用该方法 - Flask - POST - The method is not allowed for the requested URL 所请求的URL不允许使用该方法。 在烧瓶 - The method is not allowed for the requested URL. in Flask 如何修复 Flask 中的“请求的 URL 不允许使用该方法” - How to fix "The method is not allowed for the requested URL" in Flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM