1

I am using FullCallendar (a node package) to display a calendar, I want to display data of an event in this calendar upon clicking it, which is handled by the following function:

eventClick: function(info) {
    $.ajax({
        type: 'GET',
        url: 'get_lesson_by_id/',
        beforeSend: function (xhr) {
            xhr.setRequestHeader("XSRF-TOKEN",
                $('input:hidden[name="__RequestVerificationToken"]').val());
        },
        data: {
            current_id: info.event.id
        },
        success: function (res) {
            $("#signupModal").modal('show');
        }
    })
}

This sends a request to my view where I want to update the context:

def get_lesson_by_id(request):
    lesson_id = request.GET['current_id']
    lesson = get_object_or_404(Lesson, pk=lesson_id)
    print(lesson.to_dict())
    return render(request, 'agenda/agenda.html', lesson.to_dict())

This all works great, an example of what the view prints is this:

{'lesson_id': 2, 'lesson_title': 'Vioolbouwweek', 'lesson_begin_date': datetime.date(2022, 6, 3), 'lesson_end_date': datetime.date(2022, 6, 9), 'lesson_begin_time': datetime.time(9, 0), 'lesson_end_time': datetime.time(19, 0), 'lesson_description': 'Lorem ipsum dolor sit 
amet, consectetur adipiscing elit. Morbi lobortis neque vitae tempus ullamcorper. Ut ac dolor euismod, congue tortor eu, suscipit nunc. 
Fusce vulputate diam condimentum sem interdum fringilla. Suspendisse vitae justo vulputate, laoreet lacus quis, mattis lectus. Mauris tincidunt sit amet velit vel ultrices. Quisque tincidunt enim eu viverra dictum. Duis malesuada hendrerit ante nec commodo. Mauris condimentum, mi id gravida pretium, ante ex mollis leo, eu elementum orci lorem molestie neque.', 'lesson_type_name': 'Vioolbouwweek', 'lesson_duration_days': 7, 'lesson_teachers': ['Rob Stemerding']}

However when I try to use these new context variables in the template like so:

{{ lesson_id }}

they all show absolutely nothing.

I tried looking at the {% debug %} and the context variables show up like so:

<Var token: "lesson_title...">, <Text token: " ...">] plural=[]>, <TextNode: '\n </h3>\n '>, <Variable Node: lesson_id>, <TextNode: '\n '>, <Variable Node: lesson_teachers>, <TextNode: '\n </div>\n <'> etc...

Buy I think this is just information as to where I am trying to use the tag.

AvidProgrammer
  • 103
  • 2
  • 8

1 Answers1

1

Now that I'm coffeed up, let me take another shot at this. So, it appears that you are sending over the template to be rendered, but in your success function, you never actually do anything with it. All you do is pop open the modal.

you need to so something like $("#whatever div is").html(res) then pop open the modal

Check out number 14 on this answer: Bootstrap 3 - How to load content in modal body via AJAX?

edit - You also don't have to send a template, you can just send the variable, which is what I think you are trying to do.

something like - return JsonResponse(lesson.to_dict(),safe=False)

but you still have to tell the template where you want the changes. If you post the Modal, I should be able to help with that.

Adam James
  • 157
  • 1
  • 4
  • I tried both your first solutions (lesson.lesson_id) and {'context': lesson.to_dict()} but it didn't work, also isn't the last one what I am doing already? – AvidProgrammer Jun 04 '22 at 13:15
  • So unfortunately there is no way to get it as a Django variable, so that I have full control over it? For instance with Django filters or looping, etc – AvidProgrammer Jun 06 '22 at 08:40
  • It is a Django variable in the template once rendered. You can have your template do whatever you want with it. Is agenda.html the full template or is it a small section of HTML that you are including in a larger template? If you console.log(res). You will probably see what you are looking for. You just need to tell your template where to put it. – Adam James Jun 06 '22 at 11:39