不允许使用方法所请求的URL不允许使用该方法

不允许使用方法所请求的URL不允许使用该方法

本文介绍了Flask错误:“不允许使用方法所请求的URL不允许使用该方法"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我认为问题出在我正在执行的return redirect(url_for('database'))中.我也尝试过return render_template('database.html).表单项提交到数据库后,我正在尝试调用数据库页面.

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.

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

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是...

entry.html is...

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>

和database.html ...

and database.html...

THIS IS THE QUERY:

{{query}}

推荐答案

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

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

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

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

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

这篇关于Flask错误:“不允许使用方法所请求的URL不允许使用该方法"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:24