Skip to content

Instantly share code, notes, and snippets.

@Rigil-Kent
Created May 9, 2019 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rigil-Kent/ae90e8c9c90376a6e754569b1cb9ae74 to your computer and use it in GitHub Desktop.
Save Rigil-Kent/ae90e8c9c90376a6e754569b1cb9ae74 to your computer and use it in GitHub Desktop.
Datetime Flask-TempusDominus
<div class="container">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<div class="input-group date" id="datetimepicker1" data-target-input="nearest">
<input type="text" name="date" id="datetimepicker1" class="form-control datetimepicker-input" data-target="#datetimepicker1"/>
<div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
</div>
<!-- Make sure the format matches the format in forms.py -->
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
});
$('.datetimepicker-input').each(function() {
var value = $(this).val();
$(this).datetimepicker({
format: 'MM-DD-YYY HH:MM p',
date: value
});
$(this).val(value);
});
</script>
</div>
</div>
<div class="col">
{{ form.submit(class="btn btn-success") }}
</form>
class TestForm(FlaskForm):
# not setting the format will prevent the form from grabbing the datetime value
date = DateTimeField('Date & Time', format='%m/%d/%Y %H:%M %p')
submit = SubmitField("Submit")
@app.route('/test/datetime', methods=["GET", "POST"])
def test_datetime():
form = TestForm()
if form.validate_on_submit():
date = form.date.data
return redirect(request.url)
return render_template('test/datetime.html', form=form)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment