私はFlaskにウェブサイトを構築しています。私は入力を受け取り、データベースからレコードをレンダリングするフォームを作った。私はデータベースから取得したレコードに加えられた変更を処理するのに助けが必要です。たとえば、ユーザが月曜日に1pmを入力し、日曜日に2pmを入力したとします。フォームは入力を処理して表示します。しかし、午後1時から午後3時に変更したい場合、私はできません。インラインでの編集はできますが、データベースへの変更はコミットしません。 次のファイルは以下の通りです:データベースにインライン編集をコミットする方法
schedule.html:
{% for r in result %}
<div class="form-inline">
<form class="form-schedule" method="post" action="/schedule/update">
<div class="form-group">
<input type="text" id="inputMonday" name="monday" class="form-control" value="{{ r.monday }}" required autofocus>
<div class="form-group">
<input type="text" id="inputSunday" name="sunday" class="form-control" value="{{ r.sunday }}" required autofocus>
</div>
{% endfor %}
</br>
<button class="btn btn-lg btn-primary" type="submit">Save</button>
</form>
views.py:
@home.route('/schedule')
@login_required
def schedule():
"""
Render the schedule template on the /schedule route
"""
result = Schedule.query.filter_by(persons_id = current_user.id)
return render_template('home/schedule.html', title="Schedule", result=result)
@home.route('/schedule/update', methods=['POST', 'GET'])
@login_required
def schedule_update():
"""
Process the schedule then render schedule route
"""
monday = request.form['monday']
sunday = request.form['sunday']
persons_id = 1
employees_id = 1
days = Schedule(monday=monday, sunday=sunday, persons_id=persons_id, employees_id=employees_id)
db.session.add(days)
db.session.commit()
return redirect(url_for('home.schedule'))
これは機能します。私はスケジュール表の1行を変更することができます!私はいくつかのコードを追加して、より多くの行を生成し、それらのために働くよう調整します。しかし、本当にありがとう。私がStackOverflowを信用しなくなっていたときに、あなたは私に大きな返信をしました。 – Eden
@エデン - この回答を受け入れてください。 – Martlark