【问题标题】:Failed lookup for key [start_index]查找键 [start_index] 失败
【发布时间】:2020-01-27 20:56:00
【问题描述】:

我的分页有问题,这是我的代码模板

{% for post in post %}
    <tr class="text-center">
  <td>
    {{forloop.counter|add:post.start_index }} 
 </td>
 {%endfor%}

view.py

def rankpag(request):
with connection.cursor() as cursor:
    cursor.execute("SELECT c.CharName,gu.GuildName,c.Level,c.Family,c.Sex,c.Job,c.K1,c.K2,Str,Dex,Rec,Int,Luc,Wis,HP,MP,SP,g.Country FROM PS_GameData.dbo.Chars c JOIN PS_UserData.dbo.Users_Master m ON m.UserUID=c.UserUID JOIN PS_GameData.dbo.UserMaxGrow g ON g.UserUID=c.UserUID LEFT JOIN PS_GameData.dbo.GuildChars gc ON (gc.CharID=c.CharID and gc.Del=0) LEFT JOIN PS_GameData.dbo.Guilds gu ON gu.GuildID=gc.GuildID ORDER BY c.K1 DESC")
    row = cursor.fetchall()
    paginator = Paginator(row,10)
    page = request.GET.get('page')
    post = paginator.get_page(page)
return render(request,'Ranks.html',{'post':post})

我添加了一个分页,它运行良好,但我想计算行数,这就是我所看到的

【问题讨论】:

  • 嗨,请将 for 循环更改为:{% for po in post %} {{forloop.counter|add:po.start_index }} {%endfor%}
  • for 循环变量的名称不应与基本变量相同。

标签: python django django-templates django-pagination


【解决方案1】:

请将变量名 post 更改为 item。循环变量不应该。

{% for item in post %}
    <tr class="text-center">
  <td>
    {{forloop.counter|item.start_index }} 
 </td>
 {% endfor %}

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签