![](/img/trans.png)
[英]Flask-WTF form doesn't have attribute 'validate_on_submit'
[英]Why my validate_on_submit() doesn't work?
填写表格并单击“提交”后,文件未保存。 表单是“提交失败”是有效的。 已经用了 3 个小时,还是找不到问题
主文件
def new_add_payment(id,summ,less,typer):
timer = datetime.datetime.now()
f = codecs.open(os.path.abspath('invoices') + '/' + str(id) + '$' + str(timer) + '.json', 'w', 'utf-8')
payment = {'id': id, 'summ': summ, 'less': less, 'type': typer}
f.write(str(payment))
在路线中尝试不同的组合,但它们不起作用。
路线.py
@app.route('/new_payment',methods = ['GET','POST',])
def new_add_payment():
form = AddPaymentNew()
if form.validate_on_submit():
id = format(form.sub.data)
summ = format(form.summ.data)
lessons = format(form.summ.data)
typer = request.form['options']
main.new_add_payment(id,summ,lessons,typer)
print('Submit successful')
return render_template('ok.html')
else:
print('Fail to submit')
return render_template('payment_new.html', title='NEW ADD PAYMENT',form=form)
我想主要问题出在 HTML 文件中,但我想不通
Payments_new.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta name="Generator" content="Wolf 1.42">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="keywords" content="">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" charset="UTF-8">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js" charset="UTF-8"></script>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Actor|Lato:300|&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<title>Payment</title>
</head>
<style type=text/css>
@import url('https://fonts.googleapis.com/css?family=Comfortaa&display=swap');
.new1 {font-family:'Comfortaa'}
.new2 {background-image:url(../static/ach/bg1.png)}
</style>
<body class="new2">
<h1 class="new1" align="center">Оплата</h1>
<form action="{{ new_add_payment }}">
{{form.csrf_token}}
<p class="new1" align="center">
{{ form.sub.label }}<br>
{{ form.sub(size=32) }}<br>
</p>
<p class="new1" align="center">
{{ form.summ.label }}<br>
{{ form.summ(size=32) }}<br>
</p>
<p class="new1" align="center">
<input type="radio" name="options" id="option1" value="1"> 1 |
<input type="radio" name="options" id="option2" value="2"> 2 |
<input type="radio" name="options" id="option5" value="5"> 3 |
<input type="radio" name="options" id="option3" value="3"> 4 |
<input type="radio" name="options" id="option4" value="4"> 5 |
<input type="radio" name="options" id="option6" value="6"> 6
</p>
<p class="new1" align="center">
{{ form.lessons.label }}<br>
{{ form.lessons(size=32) }}<br>
</p>
<p class = 'new1' align="center">{{form.submit()}}</p>
<p class="new1" align="center"><a href="menu">Back</a></p>
<p class="new1" align="center">Payments v.1.1</p>
</form>
</body>
这里没有足够的信息 - 您没有显示表单定义。 为什么不在调用 submit 之前打印出表单的内容(或使用调试器)以确保您的路由正常工作,并且您获得的数据就是您的想法。
使用您的附加信息 - 您的提交按钮不应有任何验证器(因为它不传输任何数据)。 另外 - form.errors 列出了遇到的所有错误 - 按字段 - 我会打印出来。
要准确检查验证失败的位置,您可以在模板中添加这段代码以轻松进行调试过程
{% for error in form.errors %}
<div class="alert alert-danger alert-dismissible">
<p><i class="icon fas fa-ban"></i> Alert!</p>
{{ error }} invalid
</div>
{% endfor %}
除此之外,您的表单没有分配给
<form action="/controller_method/" method="post">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.