Python 在django模板上渲染数组
我有以下看法Python 在django模板上渲染数组,python,django,templates,orm,Python,Django,Templates,Orm,我有以下看法 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',
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
中,我有一个字段state\u id,其中三个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>
如何为数据库中的1,2,3渲染该值?在您的
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>
{{Item.get\u current\u state.display}
详细说明了这是如何工作的
<td>{{ Item.get_current_state.display }}</td>