2013-02-12 43 views
0

我有一個Django的網站,我用我的views.py文件,像這樣的列表:的Python blocktrans與不工作

EventsList = [] 

EventsList.append({'ipaddress':'192.168.200.1', 'starttime':'02/12/2013 13:59:42', 'endtime':'02/12/2013 14:59:42'}); 

EventsList.append({'ipaddress':'192.168.200.2', 'starttime':'02/12/2013 13:59:42', 'endtime':'02/12/2013 14:59:42'}); 

EventsList.append({'ipaddress':'192.168.200.3', 'starttime':'02/12/2013 13:59:42', 'endtime':'02/12/2013 14:59:42'}); 

在我的HTML頁面,我要做到以下幾點:

<p><b>{% blocktrans with len(EventsList) as Count %}{{ Count }}</b> events detected from <b>{{ first_session_start_time }}</b> to <b>{{ last_session_end_time }}</b>.{% endblocktrans %}</p> 

,但我得到的頁面加載錯誤:

Exception Type: TemplateSyntaxError 
Exception Value:  
Could not parse the remainder: '(Events)' from 'len(Events)' 

如何解決這個問題?

回答

1

你做錯了。在模板中,您需要length模板標籤:

<p><b>{% blocktrans with EventsList|length as Count %}{{ Count }}</b> events detected from <b>{{ first_session_start_time }}</b> to <b>{{ last_session_end_time }}</b>.{% endblocktrans %}</p> 

在模板,你不能執行Python代碼。模板語言有自己處理事物的方式。

+0

謝謝,事件列表|長度是報告,它是長度爲0,當其實有幾個項目。 – user1646528 2013-02-12 18:25:50

+0

你是否在請求上下文中正確傳遞了'EventsList'? – 2013-02-12 18:46:37

+0

我傳遞它像這樣:EventsList = [] EventsList.append({'ipaddress':'192.168.200.1','starttime':'02/12/2013 13:59:42','endtime':' 02/12/2013 14:59:42'})response_dict ['EventsList'] = EventsList return render_to_response('mypage.html',response_dict,context_instance = RequestContext(request)) – user1646528 2013-02-13 11:47:13