Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.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 无法访问模板中的字典值(对象pk是dict键)_Django_Dictionary_Django Templates_Django Views - Fatal编程技术网

Django 无法访问模板中的字典值(对象pk是dict键)

Django 无法访问模板中的字典值(对象pk是dict键),django,dictionary,django-templates,django-views,Django,Dictionary,Django Templates,Django Views,我有一个Django视图,它将字典构造为模板。我见过类似的问题,但没有人演示如何使用对象pk作为键访问模板中的字典值(在我的例子中,键s是对象的pks) 查看构造dict的代码: comment_uservote = {} if not current_logged_user.is_anonymous(): for comment in comments_all: try: co_vote = Vote.objects.get(user=cur

我有一个Django视图,它将字典构造为模板。我见过类似的问题,但没有人演示如何使用对象
pk
作为键访问模板中的字典值(在我的例子中,
s是对象的
pk
s)

查看构造dict的代码:

comment_uservote = {}
  if not current_logged_user.is_anonymous():
    for comment in comments_all:
        try:
            co_vote = Vote.objects.get(user=current_logged_user, comment=comment)
            comment_uservote[comment.id] = co_vote.vote
        except Vote.DoesNotExist:
            co_vote = ''
            comment_uservote[comment.id] = co_vote
我也尝试过
comment\u uservote[str(comment.id)]
,但这也没有帮助

模板(不起作用):


谢谢你的帮助。如果您需要我提供更多信息,请告诉我。

不,这不起作用,文档中也没有暗示应该这样做。您需要一个自定义标记或过滤器。

对于Django来说是相当新的。在标记和过滤器之间,哪一个更容易实现。任何针对我的情况的提示都是有益的,因为我不知道从哪里开始。你有没有什么文章可以指给我一个提示,对我的情况有帮助?
{% for comment in comments %}
  {{comment_uservote.comment.pk}} <!--this does not work-->
{% enfor %}
{% for comment in comments %}
  {{comment_uservote.16}} <!--this works-->
{% enfor %}