首页 > 解决方案 > 不允许使用烧瓶方法

问题描述

我正在开发一个烧瓶项目并遇到方法未找到错误。我只是想在按下提交按钮时从我的主页转到我的游戏页面。我不知道我哪里出错了。

我知道这个问题已经被问过好几次了,但我已经尝试了这些问题中的所有内容,但仍然出现错误。

这是我的路线文件:

from flask import render_template
from app import app
from app.forms import LoginForm

@app.route('/')
@app.route('/homepage', methods=['GET', 'POST'])
def homepage():
    form = LoginForm()
    if form.validate_on_submit():
        return redirect(url_for('index'))
    return render_template('homepage.html', title='Welcome', form=form)

@app.route('/index')
def index():

    return render_template('index.html')

这是我在主页上包含该按钮的 html 代码:

<html>
    <head>
        {% if title %}
            <title>{{ title }} - CatanAI</title>
        {% else %}
            <title>CatanAI</title>
        {% endif %}
    </head>
        <h1>Welcome to Settlers of Catan AI</h1>
    <body>

        <form method="post" action='{{url_for('index')}}'>

        <p>{{ form.submit() }}</p>
        </form>
    </body>
</html>

这是我试图路由到的代码的html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Catan</title>
        <script src="{{ url_for('static', filename="css/main.css")}}"></script>
    </head>
    <body>

        <canvas id="canvas" ></canvas>
        <script src="{{ url_for('static', filename="js/board.js")}}"></script>
        <script src="{{ url_for('static', filename="js/game.js")}}"></script>
        <script src="{{ url_for('static', filename="js/location.js")}}"></script>
        <script src="{{ url_for('static', filename="js/main.js")}}"></script>
        <script src="{{ url_for('static', filename="js/player.js")}}"></script>
    </body>
</html>

当我按下按钮时,我得到了不允许的 405 方法。我很感激任何帮助。

标签: pythonhtmlflaskhttp-status-code-405

解决方案


索引视图不允许发布方法。

你需要更换

<form method="post" action='{{url_for('index')}}'>

经过

<form method="post" action='{{url_for('homepage')}}'>

如果要在索引 url 中执行 post 方法,则需要将 methods=['GET', 'POST'] 添加到索引视图中,如下所示:

@app.route('/index', methods=['GET', 'POST'])
def index():
    # write your code here
    return render_template('index.html')

推荐阅读