2015-10-30 50 views
-1

我試圖在Django模板中顯示客戶評論的星號。無法解析其餘部分:'= customerreview.stars'from'val = customerreview.stars'

當我嘗試值賦值給一個變量 {{val = customerreview.stars}}

我收到此錯誤

Could not parse the remainder: ' = customerreview.stars' from 'val = customerreview.stars'

下面是mycode的

{% for customerreview in customerreviews %} 
<div class=item> 
    <div class="rx-client-reviews rx-pading-none"> 
     <div class="rx-client-img rx-pading-none"> 
      <img src="img/main01.jpg" alt> 
     </div> 
     <div class=rx-pading-none> 
      <h5>{{customerreview.name}}</h5> 
      <h5><a href="index03.html#">{{customerreview.dasignation}}</a></h5> 
      <p>{{customerreview.message}}</p> 
      <div class=rx-rating> 
       <ul> 
        {{val = customerreview.stars}} 
        {% for i in val%} 
        <li><span class="icon icon-Star"></span></li> 
        {% endfor %} 

        <li> 
         <p class=rating-text>{{customerreview.stars}} star rating</p> 
        </li> 
       </ul> 
      </div> 
     </div> 
    </div> 
</div> 
{% endfor%} 

有人可以幫我解決這個問題。 TNX。

回答

1

您應該使用with tempate標籤:

{% with val=customerreview.stars %} 
    ... 
{% endwith %} 

參考:Django documentation: Built-in template tags and filters

+0

TNX。但是,當我嘗試循環val的值。我得到錯誤''長'對象不可迭代' –

+1

嗯,這可能是因爲'customerreview.stars'是一個很長的數字,而不是例如一個列表。這與使用'with'無關。關於如何在這裏循環這裏有一些可能性:http://stackoverflow.com/questions/1107737/numeric-for-loop-in-django-templates – Wtower

相關問題