我有这个表单,每次我尝试将它发布到问题的底部的方法。该应用程序直接给我一个405错误。我已经通过在控制台中使用相同的方法检查了我的数据库是否正常工作。
(我删除了很多html,因为它只是样式和类似的东西)
#!/bin/sh
`clear`
num_lines=`tput lines`
num_cols=`tput cols`
echo "Enter your Name: "
read name
length_name=`echo $name | wc -c `
length_name=`expr $length_name - 1`
offset=`expr $length_name / 2`
x_center=`expr $num_lines / 2`
y_center=`expr $num_cols / 2`
y_center=`expr $offset + $x_center`
printf "%s = %d, %s = %d\n" "X" "$x_center" "Y" "$y_center"
echo -n "\033[$x_center;$y_centerf" $name
我用html post表单调用的python方法:
<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>
RegistrationForm方法如下所示:
@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')
答案 0 :(得分:0)
尝试更改
if request.method == 'POST' and form.validate():
到
if form.validate_on_submit():
答案 1 :(得分:0)
如果这确实是您正在使用的代码,我无法理解为什么您会收到405 Unsupported Method
而不是验证错误。您需要以html格式呈现隐藏的CSRF字段。
<form ...>
{{ form.hidden_tag() }}
此代码还有其他问题。重定向到登录,不要从寄存器视图中呈现模板。使用form.validate_on_submit()
。不要将request.form
传递给表单。让表单呈现输入,不要自己编写html。
答案 2 :(得分:0)
除非你的当前应用程序有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'