[英]rendering array on django template
I have following view 我有以下观点
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))
In the model MerchantDeal
i have one field state_id with three ids 1,2,3 for created, completed and used respectively 在模型
MerchantDeal
我有一个字段state_id,其中三个ID分别为1,2,3,分别用于创建,完成和使用
I have following template 我有以下模板
<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>
This is show current_state as 1,2,3 instead of created,completed and used. 这显示current_state为1,2,3,而不是创建,完成和使用。 I have defined stateText array too
我也定义了stateText数组
stateText = {1 : 'Created',
2 : 'Activated',
3 : 'Completed'}
How to render that for 1,2,3 which comes from database? 如何渲染来自数据库的1,2,3?
In your MerchantDeal
model: 在您的
MerchantDeal
模型中:
STATE_CHOICES = ((1,'Created'),(2,'Activated'),(3,'Completed'))
class MerchantDeal(models.Model):
# .. your various other fields
current_state = models.IntegerField(choices=STATE_CHOICES)
Then in your template: 然后在您的模板中:
<td>{{ Item.get_current_state.display }}</td>
The documentation details how this works. 文档详细说明了它是如何工作的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.