如何为每个创建的div id添加一个数字?

时间:2022-11-25 19:05:49

I am using a django for loop to pull information from a query set. Also, as it prints the information, I want it to do so with a surrounding div tag who's id increments by 1 for each instance. How do I go about this? is it possible in django or do I need javascript? if so, how? view:

我使用django for循环从查询集中提取信息。此外,当它打印信息时,我希望它使用周围的div标签来执行此操作,每个实例的id增加1。我该怎么做?是可能在django或我需要javascript?如果是这样,怎么样?视图:

 for x in y:

   Print(x)

I would like to get back: template:

我想回来:模板:

 {%for x in y%}
      {{x}}

 <button data-toggle="collapse" data-target="col">collapse</button>
 {%endfor%}

but all of my IDs come out as:

但是我的所有ID都是:

   <div id="col">x1</div>
   <div id="col">x2</div>
   <div id="col">x3</div>

If all the divs have the same ID then they will collapse and open at the same time, i want that to happen individually

如果所有div都具有相同的ID,那么它们将同时折叠并打开,我希望它能够单独发生

1 个解决方案

#1


0  

Something like this:

像这样的东西:

{% for x in y %}
  {{ x }}
  {{ forloop.counter }} #prints the number


#if y = ['a', 'b', 'c'] 
#{{ forloop.counter }} will print 1, 2, 3

#1


0  

Something like this:

像这样的东西:

{% for x in y %}
  {{ x }}
  {{ forloop.counter }} #prints the number


#if y = ['a', 'b', 'c'] 
#{{ forloop.counter }} will print 1, 2, 3