2012-11-08 130 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细节是如何工作的。