Pass python / django variable in javascript as JSON?

Woking on a personal project website with django 1.9 and python 3.4. I am using FullCalendar . The idea is to pass a set of destination objects to an html page containing javascript for the calendar. But right now I'm just trying to accept one default assignment.

In views.py , I have the following:

appt = json.dumps({ "title": "meeting", "start": "2016-11-20"});
return render(request, 'healthnet/profile_patient.html', {'patient': patient, 'appt': appt_set})

In profile_patient.html :

<script>

    var data = jQuery.parseJSON("{{appt}}");

    var events;
    events = [];
    events.push(data);


    $(document).ready(function() {

        $('#calendar').fullCalendar({
            editable: true,
            eventLimit: true, // allow "more" link when too many events
            events: events
        });
    });
</script>

apptIt does not work out correctly. When a web page loads, the calendar does not appear at all.

When I substitute a apptstraight line, it works:

var data = jQuery.parseJSON('{"title": "meeting", "start": "2016-11-20"}');

When I call alert("{{appt}}");, I get the following:

enter image description here

So there is something wrong with the way I use varbale. Any ideas?

+4
1

:

var data = jQuery.parseJSON('{{appt | safe}}');

n.b.

var apptData = {{ appt | safe }};
+2

Source: https://habr.com/ru/post/1661390/


All Articles