In what ways can I incorporate Django template tags into my JavaScript code?

I've encountered an issue with implementing my model data into a FullCalendar library template in JavaScript. Despite seeing others do the same successfully, I keep getting errors as the template tags fail to register properly.

<script>
document.addEventListener('DOMContentLoaded', function() {
var Calendar = FullCalendar.Calendar;
var Draggable = FullCalendarInteraction.Draggable;

var containerEl = document.getElementById('external-events');
var calendarEl = document.getElementById('calendar');
var checkbox = document.getElementById('drop-remove');

// initialize the calendar
// -----------------------------------------------------------------

var calendar = new Calendar(calendarEl,  {
  plugins: [ 'interaction', 'dayGrid', 'timeGrid', 'bootstrap', 'interaction' ],
  themeSystem: 'bootstrap',
  selectable: true,
  select: function(info) {

    var titleStr = prompt('Enter Title');
        var date = new Date(info.startStr + 'T00:00:00'); // will be in local time

        if (!isNaN(date.valueOf())) { // valid?
          calendar.addEvent({
            title: titleStr,
            start: date,
            allDay: true,
          });
        } 
  },
  locale: "sv",
  header: {
    left: 'prev,next today',
    right: 'dayGridMonth,timeGridWeek,timeGridDay'
  },
  customButtons: {

  },
  eventClick: function(info) {
    alert('Event: ' + info.event.title);

  },
  editable: true,
  droppable: true, 
  events: [

  {% for event in events %}
                    {
                        title: "{{ event.name}}",
                        start: '{{ event.start|date:"Y-m-d" }}',
                        end: '{{ event.end|date:"Y-m-d" }}',

                    },
                {% endfor %}

  ],

 });

  calendar.render();
  });

  </script>

My challenge lies with the {% for event in events %} loop where the model data does not seem to translate into the JavaScript template correctly. While I can display the data within HTML tags, integrating it into the JavaScript code remains problematic. Would appreciate any assistance on this matter.

The approach taken in these posts appears to be successful: FullCalendar in Django

Answer №1

Although the question may be a bit old, I still want to provide an answer.

This solution has worked well for me.

events: [
          {% for event in events %}
             {
                  title: "{{ event.event_name}}",
                  start: '{{ event.start_date|date:"Y-m-d" }}T{{ i.start_date|time:"H:i"}}',
                  end: '{{ event.end_date|date:"Y-m-d" }}T{{ event.end_date|time:"H:i" }}',
              },
          {% endfor %}
        ]

Could you please share your code from views.py? Thank you!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Vue.js class bound to the object remains static even after the object is updated

When I have a number of buttons, generated using the v-for directive, with each button having an initial class based on a string from an object. There is an event that changes this string when a button is clicked. However, the class does not get updated a ...

Order of execution behavior

I am currently working on a function that, when triggered, will populate the webpage with dynamic tiles. These tiles retrieve data from a remote database through an AJAX request and utilize jQuery 3.0 in the implementation. Function Implementation: funct ...

Ensure Focus Retention Upon Clicking Inside Iframe with li a:focus

How can I prevent my ul.SideNav_Main li a:focus class from losing focus when I click on the iframe or elsewhere on the page? Shouldn't it maintain focus until I click on another URL in the list? Is it possible to solve this issue with just CSS, or wo ...

Oops, it seems like the project is missing a `pages` directory. Please kindly create one in the project root. Thank you!

Initially, my project setup looked like this: public .next src pages components assets next.config.js It was functioning properly, but I made a structural change to the following: public src client next.config.js jsconfig.json pa ...

jquery logic for iterating through all elements in a select menu encountering issues

In search of a solution to iterate through all options in a dropdown list using code, comparing each to a variable. When a match is found, I aim to set that particular value as the selected item in the dropdown and then exit the loop. Here's what I&ap ...

What is the best way to incorporate a hyperlink in a React project using Tailwind

I am working on a project using React and Tailwind CSS. I want to be able to redirect the user to the next page when they click on a link. I have tried using the <a /> tag, but I'm not seeing any noticeable difference between that and the <p ...

Utilizing a custom filter for object manipulation within Vuetify's v-autocomplete

When using vuetify v-autocomplete with an object where the object consists of Key-Value pairs, the search functionality is based on the item-text. In my example, I am displaying the object as {1200-Chloride Systems}. Is it possible to make the search funct ...

Setting bootstrap datetimepicker values dynamically from a variable

I'm trying to utilize the inline bootstrap datetimepicker from At the moment, it opens in a popup window after the user selects a date/time from a table on another page. My goal is to pass the date/time from that page into datetimepicker and have it ...

An error message indicating that the page is currently being unloaded has appeared

While working on a NodeJS-ReactJS Isomorphic App, I encountered an issue when clicking on a Link. An error message popped up saying: Uncaught (in promise) Error: Request has been terminated Possible causes: the network is offline, Origin is not allowed by ...

Unusual CSS media queries behavior

During my project work, I faced a puzzling issue which can be illustrated with the following example: Here is the sample CSS code: *, *::after, *::before { box-sizing: border-box; margin: 0; border: 0; } @media only screen and (max-width ...

What is the best way to keep a header row in place while scrolling?

I am looking to keep the "top" row of the header fixed or stuck during page scrolling, while excluding the middle and bottom rows. I have already created a separate class for the top row in my header code: view image description ...

problem with the height of the side navigation bar

Currently, I am in the process of creating a back-end system that includes a side navigation bar positioned on the left-hand side. To accomplish this, I have opted to utilize the Bulma CSS framework and integrate it with Laravel. Although I have implemen ...

Guide to locating and substituting two integer values within a string using JavaScript

Given a string such as: "Total duration: 5 days and 10 hours", where there are always two integers in the order of days and then hours. If I need to update the old string based on calculations using fields and other values, what is the most efficient meth ...

Add a Page to Your Domain Name using the Text Input Box

I'm looking to create an input field that allows users to enter a text string, which will be added to a domain name when submitted, redirecting the user to a specific page. Here's how the process works: The user enters 'foo' into the ...

Improving Java Performance by frequently replacing one String with another

Currently, I am developing an HttpServlet extension (plugin) for a CMS that is responsible for filtering the HTML response. Within my method filterResponse, I retrieve the requested text/html as a String, which is one of three parameters. The main task a ...

Caution: Exercise caution when rendering components in React due to unstable_flushDiscreteUpdates

Trying to utilize map to render a component, but encountering a warning: Warning: unstable_flushDiscreteUpdates: Cannot flush updates when React is already rendering. MyBooks.js import React, { useState, useEffect } from 'react'; import Action ...

Ways to navigate a div within an iframe that has been loaded

As I load a page(A) inside an iframe, the HTML structure of the embedded content is as follows: <html><body> <div id="div1"></div> <div id="div2"><button>Hello</button></div> </body></html> The ...

Real-time Updating of ChartJS Charts in Rails Using AJAX

I am currently working with Rails 5 and the latest version of ChartJS library (). My goal is to retrieve the most recent 20 items from the SensorReading model and update the Chart using setInterval and AJAX. I have successfully created the AJAX call and ...

Deactivate the node-xmpp client

I have been exploring the functionalities of node-xmpp and node-simple-xmpp in order to create a basic client. Everything seems to be working well, except for the disconnection. Following the example from simple-xmpp, I have created the following file: ...

What is the best way to customize the endIcon of a styled-component button?

Presented here is a neatly styled button I've created import styled from 'styled-components'; import Button from '@material-ui/core/Button'; import ArrowForwardIosIcon from '@material-ui/icons/ArrowForwardIos'; const MyB ...