{% for term in terms %}
<div class="term term-count-{{loop.index}}">
<b>{{ term }}</b>: {{ terms[term] }}
</div>
{% endfor %}
'terms' 是一個字典,其值為 Python 中的串列:
terms = {'a':['1','2','3'], 'b':['4','5','6'], 'c': ['x', 'y', 'z']}
當前的 html 代碼將在 for 回圈中如下顯示“terms”:
a: ['1','2','3']
b: ['4','5','6']
c: ['x', 'y', 'z']
我希望按如下方式洗掉引號:
a: [1, 2, 3]
b: [4, 5, 6]
c: [x, y, z]
有沒有辦法在 html 塊中運行字串洗掉功能?如果沒有,是否還有其他可能的方式來按我的預期顯示?這是在 Flask 專案中。
uj5u.com熱心網友回復:
將您的 html 更改為
{% for key, value in terms.items() %}
<div class="term term-count-{{loop.index}}">
<b>{{ key }}</b>: [{{ value|join(', ') }}]
</div>
{% endfor %}
它將呈現為:
<div class="term term-count-1">
<b>a</b>: [1, 2, 3]
</div>
<div class="term term-count-2">
<b>b</b>: [4, 5, 6]
</div>
<div class="term term-count-3">
<b>c</b>: [x, y, z]
</div>
uj5u.com熱心網友回復:
如果您想不帶 '' 顯示,則需要將其顯示為串列的一個元素。
a = ['1','2']
So you will need to do like
<h1>
{{a[0]}}
</h1>
或者您可以執行以下操作。這不是最好的方法,但這確實可以洗掉 ''. 從串列中,代碼是:
https://github.com/Programmer-RD-AI/Stackoverflow-Questions/tree/main/Is there a way to do string removing in html tags?
檢查上面的鏈接。您可以從那里獲取代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/316753.html
