2017-02-01 72 views
0

我有一個函數,我將其傳遞給views.py中的上下文,並在django模板中用於在javascript圖表中使用。Django:生成字符串列表

功能:

def cleaning_data_plotly(area_id): 
    _input = pricecleaning.values('percent_of_obs','price_cleaning',) 
    data = [] 
    for row in _input: 
     data.append(str("Price of cleaning is " + str(row['price_cleaning']) + " USD in " + str(percent_of_obs)+" % of cases")) 
return data 

模板:

text: {{ price_cleaning }}, 

當我檢查輸出我看到這一點:

text: ['Price of cleaning is 68 USD in 3.58 % of cases ', 'Price of cleaning is 78 USD in 7.58 % of cases '], 

而我預計:

text: ["Price of cleaning is 68 USD in 3.58 % of cases", "Price of cleaning is 78 USD in 7.58 % of cases"], 

如何獲得正確的字符串?任何幫助深表感謝!

我使用Python 3.5和Django 1.10。

回答

1

如果您不想看到像那樣轉義的引號,請添加| safe

{{ price_cleaning | safe }} 

From the docs, the safe filter

標記的字符串爲不需要進一步HTML之前輸出漏出。自動轉義關閉時,此過濾器不起作用。