Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何统计和显示数据库表条目Django_Python_Django - Fatal编程技术网

Python 如何统计和显示数据库表条目Django

Python 如何统计和显示数据库表条目Django,python,django,Python,Django,我正在使用chartsjs生成图表。 我已将图表标签设置为: Index.py labels: [ {% for number in the_numbers_from_Floating %} "week " + {{ number.pk }}, {% endfor %} ], def index(request): the_numbers_from_Floating = Floating.objects.all() return render(

我正在使用chartsjs生成图表。 我已将图表标签设置为:

Index.py

 labels: [

  {% for number in the_numbers_from_Floating %}
      "week " + {{ number.pk }},
   {% endfor %}

],
def index(request):
the_numbers_from_Floating = Floating.objects.all()

return render(request, "index.html", {
    'the_numbers_from_Floating': the_numbers_from_Floating,
                                      })
视图.py

 labels: [

  {% for number in the_numbers_from_Floating %}
      "week " + {{ number.pk }},
   {% endfor %}

],
def index(request):
the_numbers_from_Floating = Floating.objects.all()

return render(request, "index.html", {
    'the_numbers_from_Floating': the_numbers_from_Floating,
                                      })
我的问题是,我正在将标签设置为数据库表的
pk
。如果删除条目并添加新条目,则
pk
值升高。因此,我扔掉了下面图片中的标签。要解决这个问题,我应该设置它,以便迭代到表的
count()
。但是我怎么能做到呢


您可以使用范围定义标签索引:

def index(request):

    the_numbers_from_Floating = Floating.objects.all()

    return render(request, "index.html", {
        'the_numbers_from_Floating': the_numbers_from_Floating,
        'label_indexes': range(1, the_numbers_from_Floating.count()+1)
        })
然后在模板中:

 labels: [

  {% for index in label_indexes %}
      "week " + {{ index }},
   {% endfor %}

],

谢谢,这帮了大忙。