ユーザーがjQuerydatepickerを介して日付を選択できるHTMLテンプレートがあります。
選択した日付をアクションに渡すにはどうすればよいですか?
アイデアは、ユーザーが日付を選択すると、それがFlaskのroute.pyに渡されます。 app.route("/date/<date>")
calendar.html
{% block topscripts %}
<link rel="stylesheet" type="text/css" href= "{{ url_for('static',filename='styles/calendar.css') }}">
<script>
$(function() {
$("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
});
</script>
{% endblock %}
{% block content %}
<form method="post" action="{{ url_for('specific_date', date='2019-04-11') }}">
<p>Date: <input type="text" id="datepicker" name='go-to-date'></p>
<input type="hidden" name="calendar-form">
<input type="submit">
</form>
{% endblock %}
したがって、ユーザーがdatepicker
IDで日付を選択すると、その日付をに渡しurl_for
ます。現在、日付(2019-04-11
)をハードコーディングして、機能することを確認しています。ユーザーがカレンダーで選択したものに対して、その部分を動的にするにはどうすればよいですか?
...それが役立つ場合は、routes.py(default_template()
最後にテンプレートをレンダリングする関数)にあります。:
@app.route("/date/<date>/", methods=["GET", "POST"])
def specific_date(date):
print("\n\nDate:", date, "\n\n")
images = get_files_on(date)
print("\n\nSpecific date images:", images)
return default_template(date=date, image_list=images)
その/date
ようにルートにPOSTリクエストを行います。
変更点calendar.html
:
{% block content %}
<form method="post" action="{{ url_for('specific_date') }}">
<p>Date: <input type="text" id="datepicker" name='go-to-date'></p>
<input type="hidden" name="calendar-form">
<input type="submit">
</form>
{% endblock %}
date
ルートの変更:
from flask import request
# only allow POST request method
@app.route("/date/", methods=["POST"])
def specific_date():
# getting the date from the POST request
date = request.form['go-to-date']
print("\n\nDate:", date, "\n\n")
images = get_files_on(date)
print("\n\nSpecific date images:", images)
return default_template(date=date, image_list=images)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加