我正在使用 chartsjs 生成圖表。我已將我的圖表標簽設置為:索引.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, })我的問題是我將標簽設置為pk我的數據庫表。如果刪除條目并添加新條目,則該pk值會增加。因此扔掉了我在下面圖片中看到的標簽。要解決這個問題,我應該設置它以便我迭代到count()我的表。但我該怎么做呢?
1 回答

慕碼人2483693
TA貢獻1860條經驗 獲得超9個贊
您可以使用范圍定義標簽索引:
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 %}
],
添加回答
舉報
0/150
提交
取消