在Django对象列表中获取对象索引

huangapple go评论103阅读模式
英文:

Getting object index in django object list

问题

我在我的一个页面上有一个叫做comments的列表,当我在它上面进行迭代时,我想要知道每个项目的索引;我想要在这个for循环中知道comment的索引:

  1. <!-- 评论列表部分 -->
  2. <div class="card shadow my-3 p-5">
  3. <h3>评论:</h3>
  4. <!-- 检查此帖子是否存在评论 -->
  5. {% if comments.count < 1 %}
  6. 暂无评论写下第一条吧
  7. {% else %}
  8. <!-- 迭代评论并显示它们 -->
  9. {% for comment in comments %}
  10. <div id="comment-div">
  11. <span id="Comment-name">作者:{{ comment.name }}</span>
  12. <span id="Comment-date">{{ comment.datetime_created|date:'M d Y' }}</span>
  13. <p id="Comment-body">{{ comment.body }}</p>
  14. </div>
  15. {% endfor %}
  16. {% endif %}
  17. </div>

视图:

  1. class PostDetailView(FormMixin, generic.DetailView):
  2. model = Post
  3. template_name = 'blog/post_detail.html'
  4. context_object_name = 'post'
  5. form_class = CreateComment
  6. def get_context_data(self, **kwargs):
  7. context = super(PostDetailView, self).get_context_data(**kwargs) # 获取默认上下文数据
  8. context['comments'] = Comment.objects.filter(accepted=True) # 向上下文添加额外字段
  9. return context

模型:

  1. class Comment(models.Model):
  2. post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
  3. email = models.EmailField(max_length=35)
  4. name = models.CharField(max_length=50)
  5. body = models.TextField()
  6. datetime_created = models.DateTimeField(auto_now_add=True)
  7. accepted = models.BooleanField(default=False)

如果您需要进一步的翻译或有其他问题,请告诉我。

英文:

I have a list in context of one of my pages called comments and when I'm iterating on it i want to know the index of item; i want to know the index of comment in this for loop:

  1. &lt;!-- comment list section --&gt;
  2. &lt;div class=&quot;card shadow my-3 p-5&quot;&gt;
  3. &lt;h3&gt;Comments:&lt;/h3&gt;
  4. &lt;!-- checking if any comment exist on this post --&gt;
  5. {% if comments.count &lt; 1 %}
  6. No comments. write the first one!
  7. {% else %}
  8. &lt;!-- iterating on comments and showing them --&gt;
  9. {% for comment in comments %}
  10. &lt;div id=&quot;comment-div&quot;&gt;
  11. &lt;span id=&quot;Comment-name&quot;&gt;by: {{ comment.name }}&lt;/span&gt;
  12. &lt;span id=&quot;Comment-date&quot;&gt;{{ comment.datetime_created|date:&#39;M d Y&#39; }}&lt;/span&gt;
  13. &lt;p id=&quot;Comment-body&quot;&gt;{{ comment.body }}&lt;/p&gt;
  14. &lt;/div&gt;
  15. {% endfor %}
  16. {% endif %}
  17. &lt;/div&gt;

view:

  1. class PostDetailView(FormMixin, generic.DetailView):
  2. model = Post
  3. template_name = &#39;blog/post_detail.html&#39;
  4. context_object_name = &#39;post&#39;
  5. form_class = CreateComment
  6. def get_context_data(self, **kwargs):
  7. context = super(PostDetailView, self).get_context_data(**kwargs) # get the default context data
  8. context[&#39;comments&#39;] = Comment.objects.filter(accepted=True) # add extra field to the context
  9. return context

model:

  1. class Comment(models.Model):
  2. post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name=&#39;comments&#39;)
  3. email = models.EmailField(max_length=35)
  4. name = models.CharField(max_length=50)
  5. body = models.TextField()
  6. datetime_created = models.DateTimeField(auto_now_add=True)
  7. accepted = models.BooleanField(default=False)

答案1

得分: 1

你可以使用 forloop.counter 或 forloop.counter0。

  1. {% for comment in comments %}
  2. {{forloop.counter}}(从1开始)
  3. {{forloop.counter0}}(从0开始)

还有一些其他方便的工具。请查看内置的 'for' 文档

英文:

You can use foorloop.counter or forloop.counter0

  1. {% for comment in comments %}
  2. {{forloop.counter}} (begins with 1)
  3. {{forloop.counter0}} (begins at 0)

There's some other handy tools as well. Have a look at the docs for 'for' builtins

huangapple
  • 本文由 发表于 2023年6月16日 11:12:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定