gpt4 book ai didi

python - 如何将字符串转换为Django模板中的列表

转载 作者:太空宇宙 更新时间:2023-11-04 01:26:13 25 4
gpt4 key购买 nike

我想在 django 模板中将字符串转换为列表:

数据:

{u'productTitle': u'Gatsby Hard Hair Gel 150g', u'productPrice': 0.0, u'productMRP': 75.0, u'hasVariants': 0, u'productSite': u'http://www.365gorgeous.in/', u'productCategory': u'', u'currency': u'INR', u'productURL': u'http://www.365gorgeous.in/gatsby-hard-hair-gel-300g-1.html', u'productDesc': u'A Water type hair gel with is hard setting and gives hair a firm hold It is non sticky hard setting and smooth to the touch Firm hold with wet look spikes', u'productSubCategory': u'', u'availability': 0, u'image_paths': u'["full/548bc0f93037bd03340e11e8b18de33b414efbca.jpg"]'} 

我想从上面的字典中提取图像路径,但图像路径在字符串 u'["full/548bc0f93037bd03340e11e8b18de33b414efbca.jpg"]' 有什么方法可以将它转换成 >["full/548bc0f93037bd03340e11e8b18de33b414efbca.jpg"] 在模板内...我知道它可以在 View 内完成,但我可以在模板中完成吗...

最佳答案

你可以write a template filter通过 json 解码运行字符串。

{% for image_path in data.image_paths|your_custom_json_decode_filter %}
{{ image_path }}
{% endfor %}

虽然这不是一个好主意,但您为什么不这样做呢?

关于python - 如何将字符串转换为Django模板中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17702805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com