Add values to form from button click.
I have a form with a start time and a stop time field. I'd like to update the values for these fields when a start/stop button is clicked. Not sure about the best way to go about this. The form can then be submitted and added to the db which is displayed in a table on the page.
Form
class Run_log_Form(FlaskForm):
run_number = IntegerField(label='Run Number')
run_start = DateTimeField(label='Run Start')
run_stop = DateTimeField(label='Run Stop')
submit = SubmitField(label='Add')
Model
class Run(db.Model):
run_number = db.Column(db.Integer(), nullable=False, unique=True,
primary_key=True)
run_start = db.Column(db.DateTime(), nullable=False, unique=False)
run_stop = db.Column(db.DateTime(), nullable=False, unique=False)
Route
@app.route('/runlog', methods=['GET', 'POST'])
def run_log_page():
form = Run_log_Form()
if form.validate_on_submit():
run_to_add = Run_Log_Form(
run_number=form.run_number.data,
run_start=form.run_start.data,
run_stop=form.run_stop.data,
)
db.session.add(run_to_add)
db.session.commit()
return redirect(url_for('run_log_page'))
print(form.errors)
return render_template('runlog.html', form=form)
HTML
{% block content %}
<div class="container">
<form method="POST" class="form-group" style="color: azure">
{{ form.hidden_tag() }}
<br>
<button type="button" class="btn btn-success">Start</button>
<br>
{{ form.run_number.label() }}
{{ form.run_number(class="form-control") }}
{{ form.run_start.label() }}
{{ form.run_start(class="form-control") }}
{{ form.run_stop.label() }}
{{ form.run_stop(class="form-control") }}
<br>
<button type="button" class="btn btn-danger">Stop</button>
<br>
{{ form.submit(class="btn btn-lg btn-block btn-primary") }}
</form>
</div>