如何通过ajax发布到flask并更新SQLAlchemy数据库来删除事件FullCalendar



我完全被难住了。

我正在制作一个癫痫发作跟踪应用程序,并试图通过弹出窗口删除Fullcalendar中的事件。当点击弹出窗口时,事件的id应发布到@app.route("/delete"(,并且应更新SQLAlchemy数据库,删除事件,并且应再次呈现日历减去事件。

这是我对删除脚本的可怕尝试。

<script>
document.addEventListener("DOMContentLoaded", function () {
let calendarEl = document.getElementById('calendar');
let calendar = new FullCalendar.Calendar(calendarEl, {
aspectRatio: 2.25,
selectable: true,
selectHelper: true,
editable: true,
expandRows: true,
timeZone: 'Europe/London',
defaultView: 'listMonth',
themeSystem: 'bootstrap',
headerToolbar: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay,listMonth'
},
weekNumbers: true,
dayMaxEvents: true,
events: [
{% for event in events %}
{
id: '{{ event.id }}',
title: '{{event.seizure_type}} {{event.note}}',
start: '{{event.date}}',
textColor: '#000',
{% if event.seizure_type == 'Strong' %}
backgroundColor: 'red',
{% elif event.seizure_type == 'Medium' %}
backgroundColor: 'orange',
{% else %}
backgroundColor: 'yellow',
{% endif %}
},
{% endfor %}
],
eventTimeFormat: {
hour: '2-digit',
minute: '2-digit',
hour12: false
},
eventClick: function (event) {
if(confirm("Are you sure you want to delete it?")){
var event_id = event.id
$.ajax({
url:"{{ url_for('delete') }}",
type: "POST",
data: {id: event_id},
success:function()
{
alert("Event Removed")
}
})
}
}
});
calendar.render();
});
</script>

和@app.route('/delete'(

@app.route("/delete", methods=['GET', 'POST'])
def delete(id):
event_id = request.data.id
event_to_delete = Seizures.query.get(event_id)
db.session.delete(event_to_delete)
db.session.commit()
return redirect(url_for('show_calendar'))

我确信这是可能的,但我还不精通JS。

问题不在于Javascript,而在于flask@app.route。在我使用request.data.id尝试获取数据之前,我应该使用request.form['id']。该事件现在正在从FullCalendar中删除,并正在从数据库中删除。

解决方案:

@app.route("/delete", methods=['GET', 'POST'])
def delete():
event_id = None
if request.method == 'POST':
event_id = request.form['id']
event_to_delete = Seizures.query.get(event_id)
db.session.delete(event_to_delete)
db.session.commit()
return redirect(url_for('show_calendar'))

相关内容

  • 没有找到相关文章

最新更新