【问题标题】:Flask Method Not Allowed [duplicate]不允许烧瓶方法[重复]
【发布时间】:2019-10-09 12:01:01
【问题描述】:

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

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

这是我的路线文件:

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 方法。感谢您的帮助。

【问题讨论】:

    标签: python html flask http-status-code-405


    【解决方案1】:

    索引视图不允许使用 post 方法。

    你需要更换

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

    通过

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

    如果你想在 index url 中执行 post 方法,你需要添加 methods=['GET', 'POST'] 到你的 index view 中,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 2014-07-28
      • 1970-01-01
      • 2020-07-30
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多