如何在页面中自动增加结果?

2024-09-30 05:18:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了这张桌子,但我不知道怎么编号。我想要一个前10名的名单,我从数据库里得到所有的数据。我基本上想让它显示“1”和下一个“2”和下一个“3”等等。我怎样才能做到这一点?你知道吗

https://imgur.com/a/9Isf941

def home(请求): 斯特拉特数据=data.objects.all所有()[:10]

count = data.objects.count()
numbers = []

for dataCount in range(1, count + 1):
    numbers.append(dataCount)

context = {
    'data': straatData,
    'count': numbers,
}

return render(request, 'home.html', context)

我需要把最上面的数字显示为1,2,3,4。。。和结果一样多。但我不知道怎么做


Tags: 数据httpscom数据库homedataobjectscount
2条回答

不需要为此设置上下文变量。您可以在模板代码中使用forloop.counter。你知道吗

{% for item in data %}
<tr>
   <td> {{ foorlop.counter }} </td>
   <td> {{ item }} </td>
</tr>
{% endfor %}

如果要从0开始计数,请使用forloop.counter0

也许您应该使用jinja在循环中呈现表?你知道吗

在html中:

{% for num in count %}
<tr>
   <td> {{ num }} </td>
   <td> {{ data[num][0] }} </td>
   <td> {{ data[num][1] }} </td>
   <!  Im not sure how your data is formatted!  >
</tr>
{% endfor %}

如果看不到html,就很难知道当前如何呈现表。你知道吗

相关问题 更多 >

    热门问题