全日历错误呈现意外令牌

Fullcalendar error rendering Unexpected token

正在研究一个新的 django 3 项目,新手,如果我的代码不是那么干净,我很抱歉。 问题是当我尝试渲染日历时......没有事件它加载得很好,但是当加载它们时它停止渲染整个日历。 控制台出现 Uncaught SyntaxError: Unexpected token '{',但我找不到问题所在,我的循环中也没有逗号。欢迎任何帮助。 我的日历脚本:

<script>
    document.addEventListener('DOMContentLoaded', function () {
        var cUI = document.getElementById('calendar');
        var c = new FullCalendar.Calendar(cUI, {
            themeSystem: 'bootstrap',
            headerToolbar: {
                left: 'prev,next today',
                center: 'title',
                right: '',
            },
            events: {
            {% for v in vacation %}
                {
                    title: "{{ v.reason }}: {{ v.starth }}-{{ v.endh }}",
                    start: "{{ v.start | date:'Y-m-d' }}",
                    end: "{{ v.end | date:'Y-m-d' }}",
                },
            {% endfor %}
            },
        });
        c.render();
        c.setOption('locale', 'es');
    });
</script>

谢谢

您犯的语法错误在 events 对象中。因为它是一个 javascript 对象,所以它需要键值条目。这意味着您需要为您的物品提供钥匙:

events: { // <-- the `{` shows that events is an object
    keyOne: { // <-- items in objects need keys (such as `keyOne`)
        title: "{{ v.reason }}: {{ v.starth }}-{{ v.endh }}",
        start: "{{ v.start | date:'Y-m-d' }}",
        end: "{{ v.end | date:'Y-m-d' }}",
    },
    keyTwo: {
        title: "{{ v.reason }}: {{ v.starth }}-{{ v.endh }}",
        start: "{{ v.start | date:'Y-m-d' }}",
        end: "{{ v.end | date:'Y-m-d' }}",
    },
},

现在,如果您只是想在名为 events 的“变量”中列出这些对象,那么它应该是一个数组:

events: [ // <-- the `[` shows that events is an array
    { // <-- items in arrays do not need keys
        title: "{{ v.reason }}: {{ v.starth }}-{{ v.endh }}",
        start: "{{ v.start | date:'Y-m-d' }}",
        end: "{{ v.end | date:'Y-m-d' }}",
    },
    {
        title: "{{ v.reason }}: {{ v.starth }}-{{ v.endh }}",
        start: "{{ v.start | date:'Y-m-d' }}",
        end: "{{ v.end | date:'Y-m-d' }}",
    },
},