Python 烧瓶形式给出 405
Python flask form gives 405
我有这个表格,每次我尝试 post 它到问题底部的方法。该应用程序直接给我一个 405 错误。我已经通过在控制台中使用相同的方法检查了我的数据库是否正常工作。
(我删除了很多 html 因为它只是样式之类的东西)
<form action="/register" method="post" class="grid">
<div class="cell colspan3" style="margin-left:2%;">
<div class="input-control modern text">
<input type="email" name="email" id="email">
<span class="label">Email</span>
<span class="informer">Skriv din email</span>
<span class="placeholder">Email</span>
</div>
</div>
<div class="cell colspan3">
<div class="input-control modern text cell">
<input type="text" name="username" id="username" >
<span class="label">Brugernavn</span>
<span class="informer">Skriv dit brugernavn</span>
<span class="placeholder">Brugernavn</span>
</div>
</div>
<div class="cell colspan3">
<div class="input-control modern text cell">
<input type="password" name="password" id="password">
<span class="label">Password</span>
<span class="informer">Skriv dit password</span>
<span class="placeholder">Password</span>
</div>
</div>
<div class="cell colspan3">
<div class="input-control modern text cell">
<input type="password" name="passwordconfirm" id="passwordconfirm">
<span class="label">Gentag password</span>
<span class="informer">Skriv dit password igen.</span>
<span class="placeholder">Gentag password</span>
</div>
</div>
<div class="cell colspan3">
<label class="input-control checkbox ">
<input type="checkbox" name="accept_tos" id="accept_tos">
<span class="check"></span>
<span class="caption">Jeg accepterer hjemmesidens regler</span>
</label>
</div>
<div class="cell colspan3">
<div class="input-control cell">
<button class="button success" type="submit">Registrer</button>
</div>
</div>
</div>
</div>
</form>
我用 html post 形式调用的 python 方法:
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm(request.form)
if request.method == 'POST' and form.validate():
newUser = User(form.username.data, form.email.data, form.password.data)
db.session.add(newUser)
db.session.commit()
return render_template('login.html')
else:
return render_template('register.html')
RegistrationForm 方法如下所示:
class RegistrationForm(Form):
username = StringField('username', [validators.Length(min=4, max=25)])
email = StringField('email', [validators.email, validators.Length(min=4, max=40)])
password = PasswordField('password',
[validators.Required(),
validators.EqualTo('confirm', message='Passwords skal matche hinanden.')
])
confirm = PasswordField('passwordconfirm')
accept_tos = BooleanField('Jeg accepterer regler for denne hjemmeside', [validators.Required()])
尝试更改
if request.method == 'POST' and form.validate():
至
if form.validate_on_submit():
如果那确实是您正在使用的代码,我不明白为什么您会收到 405 Unsupported Method
而不是验证错误。您需要在 html 表单中呈现隐藏的 CSRF 字段。
<form ...>
{{ form.hidden_tag() }}
此代码还有其他问题。重定向到登录,不要从注册视图中呈现模板。使用 form.validate_on_submit()
。不要将 request.form
传递给表单。让表单呈现输入,不要自己写 html。
显然这些代码没有任何问题,除非您的当前应用有 url_prefix,例如:
app = Blueprint('user', __name__, url_prefix='/user/')
你还有另一个 url_prefix 的蓝图,比如 :
app = Blueprint('general', __name__, url_prefix='/')
使用另一个函数,例如:
@app.route('/register', methods=['GET'])
在蓝图中...
并且您的表单正在将数据发布到“/register”而不是“/user/register”
我有这个表格,每次我尝试 post 它到问题底部的方法。该应用程序直接给我一个 405 错误。我已经通过在控制台中使用相同的方法检查了我的数据库是否正常工作。
(我删除了很多 html 因为它只是样式之类的东西)
<form action="/register" method="post" class="grid">
<div class="cell colspan3" style="margin-left:2%;">
<div class="input-control modern text">
<input type="email" name="email" id="email">
<span class="label">Email</span>
<span class="informer">Skriv din email</span>
<span class="placeholder">Email</span>
</div>
</div>
<div class="cell colspan3">
<div class="input-control modern text cell">
<input type="text" name="username" id="username" >
<span class="label">Brugernavn</span>
<span class="informer">Skriv dit brugernavn</span>
<span class="placeholder">Brugernavn</span>
</div>
</div>
<div class="cell colspan3">
<div class="input-control modern text cell">
<input type="password" name="password" id="password">
<span class="label">Password</span>
<span class="informer">Skriv dit password</span>
<span class="placeholder">Password</span>
</div>
</div>
<div class="cell colspan3">
<div class="input-control modern text cell">
<input type="password" name="passwordconfirm" id="passwordconfirm">
<span class="label">Gentag password</span>
<span class="informer">Skriv dit password igen.</span>
<span class="placeholder">Gentag password</span>
</div>
</div>
<div class="cell colspan3">
<label class="input-control checkbox ">
<input type="checkbox" name="accept_tos" id="accept_tos">
<span class="check"></span>
<span class="caption">Jeg accepterer hjemmesidens regler</span>
</label>
</div>
<div class="cell colspan3">
<div class="input-control cell">
<button class="button success" type="submit">Registrer</button>
</div>
</div>
</div>
</div>
</form>
我用 html post 形式调用的 python 方法:
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm(request.form)
if request.method == 'POST' and form.validate():
newUser = User(form.username.data, form.email.data, form.password.data)
db.session.add(newUser)
db.session.commit()
return render_template('login.html')
else:
return render_template('register.html')
RegistrationForm 方法如下所示:
class RegistrationForm(Form):
username = StringField('username', [validators.Length(min=4, max=25)])
email = StringField('email', [validators.email, validators.Length(min=4, max=40)])
password = PasswordField('password',
[validators.Required(),
validators.EqualTo('confirm', message='Passwords skal matche hinanden.')
])
confirm = PasswordField('passwordconfirm')
accept_tos = BooleanField('Jeg accepterer regler for denne hjemmeside', [validators.Required()])
尝试更改
if request.method == 'POST' and form.validate():
至
if form.validate_on_submit():
如果那确实是您正在使用的代码,我不明白为什么您会收到 405 Unsupported Method
而不是验证错误。您需要在 html 表单中呈现隐藏的 CSRF 字段。
<form ...>
{{ form.hidden_tag() }}
此代码还有其他问题。重定向到登录,不要从注册视图中呈现模板。使用 form.validate_on_submit()
。不要将 request.form
传递给表单。让表单呈现输入,不要自己写 html。
显然这些代码没有任何问题,除非您的当前应用有 url_prefix,例如:
app = Blueprint('user', __name__, url_prefix='/user/')
你还有另一个 url_prefix 的蓝图,比如 :
app = Blueprint('general', __name__, url_prefix='/')
使用另一个函数,例如:
@app.route('/register', methods=['GET'])
在蓝图中...
并且您的表单正在将数据发布到“/register”而不是“/user/register”