我正在使用 chartsjs 生成圖表。我已將我的圖表標(biāo)簽設(shè)置為:索引.py labels: [ {% for number in the_numbers_from_Floating %} "week " + {{ number.pk }}, {% endfor %}],視圖.pydef index(request):the_numbers_from_Floating = Floating.objects.all()return render(request, "index.html", { 'the_numbers_from_Floating': the_numbers_from_Floating, })我的問題是我將標(biāo)簽設(shè)置為pk我的數(shù)據(jù)庫(kù)表。如果刪除條目并添加新條目,則該pk值會(huì)增加。因此扔掉了我在下面圖片中看到的標(biāo)簽。要解決這個(gè)問題,我應(yīng)該設(shè)置它以便我迭代到count()我的表。但我該怎么做呢?
1 回答

慕碼人2483693
TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以使用范圍定義標(biāo)簽索引:
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 %}
],
添加回答
舉報(bào)
0/150
提交
取消