Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django get_comment_list返回空列表_Django_Django Templates_Django Comments - Fatal编程技术网

Django get_comment_list返回空列表

Django get_comment_list返回空列表,django,django-templates,django-comments,Django,Django Templates,Django Comments,我一直在为这件事发愁,不知道发生了什么事 在我看来,我可以做到这一点: from django.contrib.comments import Comment ... context['comments'] = Comment.objects.filter(object_pk = self.kwargs['pk']) ... 然后在我的模板中执行以下操作: {% for comment in comments %} {{ comment.comment }} {% endfor %} 它

我一直在为这件事发愁,不知道发生了什么事

在我看来,我可以做到这一点:

from django.contrib.comments import Comment

...
context['comments'] = Comment.objects.filter(object_pk = self.kwargs['pk'])

...
然后在我的模板中执行以下操作:

{% for comment in comments %}
{{ comment.comment }}
{% endfor %}
它工作完美,并显示每个评论

然而,当我尝试使用django模板标记进行注释时,我得到了一个空列表

{% load comments %}
{% get_comment_list for video as comments %}
{% for comment in comments %}
{{ comment.comment }}
{% endfor %}
{{ comment_list|length }}  // displays '0'
上面代码中的视频是模板上下文中的对象实例-我在模板的其他地方使用它,它工作正常-即{video.title}、{{video.id}

另外-其他注释模板标记似乎工作正常:

{% render_comment_list video %}
显示我在comments/list.html上找到的测试模板-此模板仅打印出“hello world”


知道这里发生了什么或如何调试吗?

只是一个猜测,但是,当您这样做时:

Comment.objects.filter(object_pk = self.kwargs['pk'])

在视图中,您没有为希望获得的注释指定
内容类型(视频),因此您正在检索id为“pk”的任何对象的所有注释-这可能就是为什么您在手动操作时会看到注释,而在将注释留给模板标记时却没有。可能注释没有附加到正确的
ContentType
-您可以在django admin中检查这一点,这只是一个猜测,但是,当您这样做时:

Comment.objects.filter(object_pk = self.kwargs['pk'])
在视图中,您没有为希望获得的注释指定
内容类型(视频),因此您正在检索id为“pk”的任何对象的所有注释-这可能就是为什么您在手动操作时会看到注释,而在将注释留给模板标记时却没有。可能注释没有附加到正确的
ContentType
——您可以在django管理员中检查这一点