简体   繁体   English

如何使用 jinja 或 javascript 将 python 中的列表转换为 python 中的逗号分隔值

[英]How to convert list from python into comma separated values in python with jinja or javascript

I want to synchronize my dropzone allowfiles templates with my python upload config allowed files.我想将我的 dropzone allowfiles 模板与我的 python 上传配置允许文件同步。 I can get jinja to spit out my UPLOAD_EXTENSIONS with config.UPLOAD_EXTENSIONS but I can not figure out how to format it into a string that can be used in my javascript allowdfiles format.我可以让 jinja 用 config.UPLOAD_EXTENSIONS 吐出我的 UPLOAD_EXTENSIONS,但我不知道如何将其格式化为可以在我的 javascript 允许文件格式中使用的字符串。 See below for what I get vs what I want.请参阅下文了解我得到的与我想要的。

jinja html template神社html模板

<script>
    let allowed_files = "{{ config.UPLOAD_EXTENSIONS }}";
    console.log(allowed_files)
</script>

Console Log控制台日志

[&#39;.jpg&#39;, &#39;.png&#39;, &#39;.gif&#39;]

What I want is this string我想要的是这个字符串

.jpg,.png,.gif

You should use safe filter like this:您应该使用这样的安全过滤器

<script>
    let allowed_files = "{{ config.UPLOAD_EXTENSIONS|safe }}";
    console.log(allowed_files)
</script>

but it returns a result like this:但它返回如下结果:

[".jpg", ".png", ".gif"]

If you want the exact use: console.log(allowed_files.join())如果您想要确切的用途: console.log(allowed_files.join())

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

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