python - 不允许使用烧瓶方法
问题描述
我正在开发一个烧瓶项目并遇到方法未找到错误。我只是想在按下提交按钮时从我的主页转到我的游戏页面。我不知道我哪里出错了。
我知道这个问题已经被问过好几次了,但我已经尝试了这些问题中的所有内容,但仍然出现错误。
这是我的路线文件:
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 方法。我很感激任何帮助。
解决方案
索引视图不允许发布方法。
你需要更换
<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')
推荐阅读
- javascript - Ionic React Scss 样式未加载
- stripe-payments - Stripe:如果您使用 Stripe 的新托管 Checkout,您可以将卡片保存给客户吗?
- azure - 如何从 Azure 密钥保管库中的证书动态获取 ARM 模板中的证书指纹?
- git - Git 大目录检出失败
- arrays - 读取输入直到值 < 0
- postgresql - 尝试使用 python 2.7 在 macosx 10.14.6 上安装 psycopg2
- ios - 当一个对象试图访问已释放的无主对象时会发生什么?
- php - 如何在页面上显示保存在 php 变量中的 JavaScript 代码?
- react-native - 我们可以在读取firebase数据库节点时删除它吗
- c++ - sendto() 在 select() 上被阻塞