简体   繁体   English

如何使用 Jinja2 将日期时间传递给模板?

[英]How to pass a datetime to a template using Jinja2?

I want to pass the variable next_start from my application.py file to the js script in a HTML template.我想将变量next_start从我的 application.py 文件传递给 HTML 模板中的 js 脚本。

start_date = datetime.datetime.strptime(input_start, '%Y-%m-%d').date()
next_start = start_date + timedelta(days=cycle_avg)
return render_template("calendar.html", next_start = next_start)

The calendar.html file script tag: calendar.html 文件脚本标签:

<script>
    let highlight = {{ next_start }};
</script>

This does not seem to work;这似乎不起作用; how can I pass this datetime ?我怎样才能通过这个datetime

No, it will not work.不,它不会起作用。 take a look at the generated script tag, and you'll see that what you've generated is not valid javascript.看看生成的脚本标签,你会发现你生成的不是有效的 javascript。 You want something that looks like你想要看起来像的东西

let highlight = new Date(....);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM