我正在为我的项目使用flask-wtforms,flask-sqlalchemy和flask-bootstrap。我不能为我的生活,理解为什么在提交表单后,它没有重定向到我设置的页面。 这是python部分:
@app.route('/newevent', methods=['GET', 'POST']) #TAGGED
@login_required
def newevent():
form = NewEvent()
msgs = ''
if request.method=='POST' and form.validate_on_submit():
check = Event.query.filter_by(eventid=form.eventid.data).first()
if check is None:
db.session.add(
Event(eventid=form.eventid.data,
eventName=form.eventName.data,
eventDate=form.eventDate.data,
allocation=form.allocation.data,
Event_orgCode=current_user.orgCode))
db.session.commit()
return redirect(url_for('adminevents'))
elif check.eventid == form.eventid.data:
msgs = 'Event already exists!'
return render_template('events_new.html', form=form, msgs=msgs)
return render_template('events_new.html', form=form, msgs=msgs)
现在这里是html部分:
{% block content %}
{% with msg = get_flashed_messages() %}
<h1>Create New Event</h1>
<h3> {{ msgs }} </h3>
<div class="input-event">
<form class="form form-horizontal" action="{{ url_for('newevent') }}" method="POST" role="form">
<dl>
{{ form.hidden_tag() }}
{{ form.csrf_token }}
{{ wtf.form_field(form.eventid, placeholder="Pick a 4-digit ID e.g., 0042") }}
{{ wtf.form_field(form.eventName, placeholder="Name your event") }}
{{ wtf.form_field(form.eventDate) }}
{{ wtf.form_field(form.allocation, placeholder="Enter budget allocation") }}
</dl>
<input class="btn btn-success" type="submit" value="Add">
<input class="btn btn-primary" type="button" value="Back" onclick="window.location.href='/adminevents'">
</form>
</div>
{% endwith %}
{% endblock %}
我有一个名为newbudget
的类似功能。它与相关代码非常相似,并且工作得非常好。请帮助我完成我的智慧。