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

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

Getting object index in django object list

问题

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

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

视图:

class PostDetailView(FormMixin, generic.DetailView):
    model = Post
    template_name = 'blog/post_detail.html'
    context_object_name = 'post'
    form_class = CreateComment

    def get_context_data(self, **kwargs):
        context = super(PostDetailView, self).get_context_data(**kwargs)  # 获取默认上下文数据
        context['comments'] = Comment.objects.filter(accepted=True)  # 向上下文添加额外字段
        return context

模型:

class Comment(models.Model):

    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
    email = models.EmailField(max_length=35)
    name = models.CharField(max_length=50)
    body = models.TextField()
    datetime_created = models.DateTimeField(auto_now_add=True)
    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:

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

view:

class PostDetailView(FormMixin, generic.DetailView):
    model = Post
    template_name = &#39;blog/post_detail.html&#39;
    context_object_name = &#39;post&#39;
    form_class = CreateComment

    def get_context_data(self, **kwargs):
        context = super(PostDetailView, self).get_context_data(**kwargs)  # get the default context data
        context[&#39;comments&#39;] = Comment.objects.filter(accepted=True)  # add extra field to the context
        return context

model:

class Comment(models.Model):

    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name=&#39;comments&#39;)
    email = models.EmailField(max_length=35)
    name = models.CharField(max_length=50)
    body = models.TextField()
    datetime_created = models.DateTimeField(auto_now_add=True)
    accepted = models.BooleanField(default=False)

答案1

得分: 1

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

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

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

英文:

You can use foorloop.counter or forloop.counter0

{% for comment in comments %}
  {{forloop.counter}} (begins with 1)
  {{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:

确定