2012-11-08 129 views
0

我具有以下視圖渲染陣列

def deals(request): 
    usr=UserProfile.objects.get(user_id=request.user.id) 
    merchant=MerchantProfile.objects.get(user_id=usr.id) 
    dealItems=MerchantDeal.objects.filter(merchant_id=merchant.id) 
    stateText = {1 : 'Created', 
        2 : 'Activated', 
        3 : 'Completed'} 

    return render_to_response('deals.html',locals(),context_instance=RequestContext(request)) 

在模型MerchantDeal我有三個ID 1,2,3爲創建的,完成了一個場STATE_ID和分別用於

我具有以下模板

<table> 
        <tr> 
        <th>Deals</th> 
        <th>Status</th> 
        <th>Start date </th> 
        <th>End date </th> 
        <th>Used</th> 
        <th>Edit</th> 
        </tr> 
        {% for Item in dealItems %} 
        <tr class="gray"> 
        <td>{{Item.title|capfirst}} </td> 
        <td >{{Item.current_state}}</td> 
        <td>{{Item.start_date}}</td> 
        <td>{{Item.end_date}}</td> 
        <td width="90">{{Item.used}}</td> 
        <td width="92"><a class="ajax cboxElement" href="/ajax/item?id={{foodItem.id}}">edit</a></td> 
        </tr> 
        {%endfor%} 
       </table> 

這是顯示current_state爲1,2,3而不是創建,完成和使用。我也已經定義了stateText數組

stateText = {1 : 'Created', 
      2 : 'Activated', 
      3 : 'Completed'} 

如何渲染對於來自數據庫的1,2,3?

回答

1

在你MerchantDeal型號:

STATE_CHOICES = ((1,'Created'),(2,'Activated'),(3,'Completed')) 

class MerchantDeal(models.Model): 
    # .. your various other fields 
    current_state = models.IntegerField(choices=STATE_CHOICES) 

然後在你的模板:

<td>{{ Item.get_current_state.display }}</td> 

documentation細節是如何工作的。