0
我試圖與瓶一個網站,並使用WTForms一個註冊頁面,像這樣我:如何訪問JQuery中的WTForm字段?
views.py:
@app.route('/register', methods=('GET', 'POST'))
def register():
form = RegistrationForm()
if form.validate_on_submit():
user = User(form.password.data)
form.populate_obj(user)
db.session.add(user)
db.session.commit()
login_user(user)
return redirect(url_for('index'))
return render_template('register.html', title='Register', form=form, user=current_user)
register.html:
{% extends "base.html" %}
{%- block content -%}
{% import "formhelpers.html" as fields %}
<div id="login_container">
<div id="register_title"><p>Register</p></div>
<form method="post" action="{{ url_for('register') }}">
{{ form.hidden_tag() }}
{{ fields.render(form.first_name, placeholder="Firstname") }} <br/>
{{ fields.render(form.last_name, placeholder="Surname") }} <br/>
{{ fields.render(form.email, placeholder="Your email") }} <br/>
{{ fields.render(form.password, placeholder="Password") }} <br/>
{{ fields.render(form.confirm, placeholder="Confirm password") }} <br/>
<p>
<input class="btn" type="submit" value="register"></a>
</p>
</form>
</div>
{ % - endblock - %}
我想要做的是給用戶一個錯誤信息,因爲他們輸入,如果他們通過字不匹配。據我所知,你可以做這樣的事情在jQuery的是這樣的:
$('.password-field').keyup(function() {
var p1 = $(this).val();
var p2 = $(.confirm-field).val();
if (p1 != p2) {
$('.error-message').show();
}
但我不知道如何訪問WTForm字段的JQuery。有任何想法嗎?