javascript - Flask/jinja 发送数组到 javascript

标签 javascript python flask fullcalendar jinja2

我有以下代码

main.py:

@app.route("/admin")
def admin_panel():
    resources = [{'id': '302', 'title': 'Participant 302'}]
    events = []
    return render_template("admin.html", admin_resources=resources, admin_events=events)

在相应的html的head中:

admin.html

$(function() {//文档准备就绪

$('#calendar').fullCalendar({
  today: "12-18-18",
  editable: true, // enable draggable events
  aspectRatio: 1.8,
  scrollTime: '00:00', // undo default 6am scrollTime
  header: {
    left: 'today,prev,next',
    center: 'title',
    right: 'timelineDay,timelineThreeDays,agendaWeek'
  },
  defaultView: 'timelineThreeDays',
  resourceLabelText: 'Participants',

  resources: {{admin_resources}},
  events: {{ admin_events}}
});

});

我收到异常,因为资源数组中的引号正在编码。当我尝试 {{admin_resources |我收到一条错误消息,指出 tojson 返回 None。

我很困惑如何解决引号被编码的问题以及为什么{{admin_resources | tojson}} 返回 None。

有什么想法吗?感谢您的任何建议!

如果我这样做,我会注意到{{admin_resources |安全}} json 似乎编码正确;但是,即使我在 html 中看到 json 数组,我也会收到以下错误:

Uncaught TypeError: Cannot read property 'resources' of undefined
    at t.n.Calendar.instantiateView (scheduler.min.js:8)
    at t.renderView (fullcalendar.min.js:10)
    at t.initialRender (fullcalendar.min.js:10)
    at t.render (fullcalendar.min.js:10)
    at HTMLDivElement.<anonymous> (fullcalendar.min.js:11)
    at Function.each (jquery.min.js:2)
    at w.fn.init.each (jquery.min.js:2)
    at w.fn.init.i.fn.fullCalendar (fullcalendar.min.js:11)
    at HTMLDocument.<anonymous> (admin:29)
    at l (jquery.min.js:2)

最佳答案

需要进行 2 项更改:

  1. 日历中今天的日期不能是“12-18-18”。需要输入正确的格式:'2018-12-18'
  2. 需要使用安全的方式进行变量访问。 {{管理资源|安全}}

关于javascript - Flask/jinja 发送数组到 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53842863/

相关文章:

javascript - 显示 :none 后重新显示 flex 布局中的 div

python - 在 Flask 应用程序中返回 Excel 文件

python-3.x - 发送多个参数到flask-restplus

python - 有没有一个函数可以再次生成代码,或者更好的想法?

python - 无法使用docker-compose在其他端口中运行Python flask应用程序

python - 如何在模板错误时跳过 Flask 缓存

javascript - 如何重命名 react-native 入口文件 (index.ios.js)

javascript - 如何让 jQuery 在关闭滑动菜单之前等待一秒钟?

javascript - 单击表格父行中的链接展开和折叠子行

python - 如何在python中发出post请求