美好的一天,我有一个简单的网页,里面有一个电子邮件表格.我正在尝试从中收集数据并填充数据库而不刷新模板.到目前为止,这是我的代码:
形成:
from flask_wtf import Form
class EmailForm(Form):
email = StringField('Email Address', [
DataRequired(message='Required field'),
Email(message='Please provide a valid email address')
])
submit = SubmitField('send')
Run Code Online (Sandbox Code Playgroud)
路线:
@app.route('/', methods=('GET', 'POST'))
def index():
form = EmailForm(request.form)
if request.method == 'POST' and form.validate_on_submit():
try:
email = Email(form.data['email'])
db.session.add(email)
db.session.commit()
except IntegrityError as e:
app.logger.info(e)
return redirect(url_for('index'))
return render_template('index.html', form=form)
Run Code Online (Sandbox Code Playgroud)
阿贾克斯:
$(function() {
$('#email_submit').bind('click', function() {
$.getJSON('/', {
email: $('input[name="email"]').val()
});
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
模板:
<form name="collectEmail" id="collectForm" method="post" action="{{ url_for('index') }}">
{{ …Run Code Online (Sandbox Code Playgroud)