方法不允许flask错误405
我正在开发flask注册表格,但收到错误消息:
error 405 method not found.
码:
import os# Flask
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash, Markup, send_from_directory, escape
from werkzeug import secure_filename
from cultura import app
# My app
from include import User
@app.route('/')
def index():
return render_template('hello.html')
@app.route('/registrazione', methods=['POST'])
def registration():
if request.method == 'POST':
username= request.form.username.data
return render_template('registration.html', username=username)
else :
return render_template('registration.html')
registration.html:
<html><head> <title>Form di registrazione </title>
</head>
<body>
{{ username }}
<form id='registration' action='/registrazione' method='post'>
<fieldset >
<legend>Registrazione utente</legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<label for='name' >Nome: </label>
<input type='text' name='name' id='name' maxlength="50" /> <br>
<label for='email' >Indirizzo mail:</label>
<input type='text' name='email' id='email' maxlength="50" />
<br>
<label for='username' >UserName*:</label>
<input type='text' name='username' id='username' maxlength="50" />
<br>
<label for='password' >Password*:</label>
<input type='password' name='password' id='password' maxlength="50" />
<br>
<input type='submit' name='Submit' value='Submit' />
</fieldset>
</form>
</body>
</html>
当我访问时localhost:5000/registrazione
,我收到错误消息。我究竟做错了什么?
回答:
这是因为在定义路由时仅允许POST请求。
当你/registrazione
在浏览器中访问时,它将首先执行GET请求。只有提交表单后,浏览器才会执行POST。因此,对于像你这样的自我提交表单,你需要同时处理两者。
使用
@app.route('/registrazione', methods=['GET', 'POST'])
应该管用。
以上是 方法不允许flask错误405 的全部内容, 来源链接: utcz.com/qa/421390.html