英文:
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:
<!-- comment list section -->
<div class="card shadow my-3 p-5">
<h3>Comments:</h3>
<!-- checking if any comment exist on this post -->
{% if comments.count < 1 %}
No comments. write the first one!
{% else %}
<!-- iterating on comments and showing them -->
{% for comment in comments %}
<div id="comment-div">
<span id="Comment-name">by: {{ 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>
view:
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) # get the default context data
context['comments'] = 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='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)
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论