【问题标题】:How to access underscore starting attributes in templates OR Error Variables and attributes may not begin with underscores in Django 2.0?如何访问模板中的下划线起始属性或错误变量和属性在 Django 2.0 中可能不以下划线开头?
【发布时间】:2018-03-13 11:33:56
【问题描述】:

我有这样的 ElasticSearch 迭代数据:

(Pdb) data[0]
     {'_index': 'publishserdata-index',
      '_type': 'publishser_data_index', 
      '_id': '35', '_score': 1.0, 
      '_source': {'publisher': 'Admin',
                 'department': 'IT', 
                 'subdepartment': 'Full Stack Developer',
                 'inventory_ID': 'IT003',
                 'title': 'Why Should I Use Celery?', }}

我想获取出版商、部门、标题数据。

当我在模板中迭代值时:-

{% for d in data %}
       {{d._source.publisher}}/error
{% endfor %}

【问题讨论】:

    标签: python django elasticsearch django-views django-2.0


    【解决方案1】:

    正如该代码所示,data[0] 是一个字典。所以你使用普通的 dict 语法来访问它的内容:data[0]['_source'],等等。

    【讨论】:

      猜你喜欢
      • 2018-06-10
      • 2018-12-22
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2014-02-24
      相关资源
      最近更新 更多