Created
May 9, 2019 18:26
-
-
Save Rigil-Kent/ae90e8c9c90376a6e754569b1cb9ae74 to your computer and use it in GitHub Desktop.
Datetime Flask-TempusDominus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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