Reputation: 3
I'm trying to write simple application in Flask/Python. But right now I dont know how save inserted date from datepicker (jQuery) into database in SQLAlchemy.
I just use a simple code to create datepicker:
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#datepicker" ).datepicker();
} );
</script>
<p>Date: <input type="text" id="datepicker"></p>
Upvotes: 0
Views: 1734
Reputation: 139
Give a name to your input form, e.g.
<input type="text" id="datepicker" name="datepicker">
Add a submit input
<input type="submit" value="submit">
or submit with jQuery like $("#datepicker").submit()
.
Then, when the form is submitted, it will be available in the Flask request context via request.form.get("datepicker")
.
Obtain that value in your view function and you should be able to save it to your database like any other value, assuming you have an appropriate model to store the data.
Also make sure that your view function allows POST method, e.g.
from flask import request
@app.route("/get_date", methods=["GET", "POST"])
def get_date():
# ...
date = request.form.get("datepicker")
# ...
Upvotes: 1