skip to Main Content

I have tried to set the cache property to false on the AJAX request, but it did absolutely nothing.
I am implementing a JS calendar and when you click on any day, it opens a popup overlay that also includes another calendar. This second calendar is opened via ajax, and everything seems to be working on the first call which renders:

first date ajax request

Then, upon closing the overlay and clicking another date on the calendar, I always get the first overlay, no matter how many times I click.

The AJAX call

$.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'); */
                    } 
                }); 

The 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) First Calendar init code

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: ' ' 
        },


      });

    }

2

Answers


  1. Chosen as BEST ANSWER

    The solution for the MDB calendar plugin is to just destroy the element and run the code again when a day is clicked.

    $('#calendar').fullCalendar('destroy');
    

    I have decided to use a free, open source calendar instead anyway.


  2. I would try adding the never_cache decorator to the view:

    from django.views.decorators.cache import never_cache
    
    
    @login_required
    @never_cache
    def teacherDayAgenda(request):
        ...
    

    See:

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search