gpt4 book ai didi

jquery - Django/AJAX - 在第一次 jQuery AJAX 调用后(即后续调用)不会呈现新数据

转载 作者:行者123 更新时间:2023-12-01 01:40:01 24 4
gpt4 key购买 nike

我尝试在 AJAX 请求上将缓存属性设置为 false,但它完全没有任何作用。我正在实现一个 JS 日历,当您单击任何一天时,它会打开一个弹出窗口,其中还包含另一个日历。第二个日历是通过 ajax 打开的,并且一切似乎都在渲染的第一个调用上正常工作:

first date ajax request

然后,在关闭叠加层并单击日历上的另一个日期后,无论单击多少次,我总是会得到第一个叠加层。

AJAX 调用

$.ajax( 
{
type:"POST",
url: "/teachers/teacherDayAgenda/",
data:{
'event_date': dateClicked
},
dataType: 'json',
cache: false,
success: function( eventData ) {

var lessonEvents = [];
var lesson = {};

for (var key in eventData) {
if (key=="eventDefault")
break;
lesson = {};
lesson['title'] = "Lesson with "+eventData[key]['teacher_name'];
lesson['start'] = formatDate(moment(eventData[key]['timeslot_start']).toDate());
lesson['end'] = formatDate(moment(lesson['start']).add(40, 'm').toDate());
lessonEvents.push(lesson);
}
console.log(eventData);
$('#teacher_schedule').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'agendaDay,listWeek'
},
defaultDate: eventData['event0']['click_event_date'],
navLinks: true,
eventLimit: true,
events: lessonEvents
});

$("#teacher_schedule .fc-agendaDay-button").trigger("click");
/*$('td').removeClass('AddEventPopup_open'); */
}
});

View :

@login_required
def teacherDayAgenda(request):
"""
Ajax request for popup overlay, changes to student schedule, see day agenda of availability of teachers
"""
if request.method == 'POST':
click_event_date = request.POST['event_date']
studentid = request.user.id
default_teacher_id = StudentProfile.objects.has_default_teacher(
studentid)


# default event values
ev = {}
d = {'eventDefault': True, 'click_event_date': click_event_date}
ev['event0'] = d

if default_teacher_id:
ev = {}
events = TeacherProfile.objects.filter(id=default_teacher_id).filter(
student_id__isnull=True).values_list('timeslot_start', 'teacher_id')
i=0
for event in events:
d = {}
d['timeslot_start'] = _datetime_to_string(event[0])
teacherName = User.objects.get_staff_name(event[1])
d['teacher_name'] = teacherName
d['click_event_date'] = click_event_date
ev['event'+str(i)] = d
i += 1


else:
first_teacher_obj = TeacherProfile.objects.get_all_available_teachers()[
0]
first_teacher_id = getattr(first_teacher_obj, 'teacher_id')
events = TeacherAvailability.get_dayagenda(
request, first_teacher_id, click_event_date)

i=0
for event in events:
d = {}
d['timeslot_start'] = _datetime_to_string(event[0])
d['student_id'] = event[1]
teacherName = User.objects.get_staff_name(event[2])
d['teacher_name'] = teacherName
d['click_event_date'] = click_event_date
ev['event'+str(i)] = d
i += 1


return JsonResponse(ev)

else:
return HttpResponse("unsuccesful")

(EDIT1)第一个日历初始化代码

function initializeCalendar(){

$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay,listWeek'
},
defaultDate: today,
navLinks: true,
editable: true,
eventLimit: true,
selectable: true,
events: [{
title: 'Simple static event',
start: '2018-11-16',
description: 'Super cool event'
},

],
customButtons: { addEventButton: { text: 'Add Event', class:'AddEventPopup_open', click: function() {
return true;
}}
},
footer: {left: ' ', center: 'addEventButton', right: ' '
},


});

}

最佳答案

我会尝试将 never_cache 装饰器添加到 View 中:

from django.views.decorators.cache import never_cache


@login_required
@never_cache
def teacherDayAgenda(request):
...

参见:

https://docs.djangoproject.com/en/3.0/topics/http/decorators/#caching

关于jquery - Django/AJAX - 在第一次 jQuery AJAX 调用后(即后续调用)不会呈现新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61182582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com