这是我模板文件中的代码
{% load section_settings %} ## Loading a template tags
{% section_settings declaration.id user.id as the_section_setting %}
现在价值:
the_section_setting = {'A': {'included': True, 'editable': True, 'alwaysIncluded': False, 'complete':False}, 'C': {'included': False, 'editable': True, 'alwaysIncluded': False, 'complete': False}}
当我尝试在js脚本中运行此代码时,现在在我的模板文件中:
<script type="text/javascript">
function initTree(showline){
{% for section in the_section_setting %}
alert("{{ the_section_setting[section]['included'] }}")
{% endfor %}
}
</script>
抛出错误'tuple' object has no attribute 'has_header'
。基本上我已经在python shell中测试了它,它对我来说很好。但不是在模板中。我在做什么错误。我想要dict中每个元素的included
键的值。
在此先感谢。
我收到以下错误:
AttributeError at /declaration/2991/detail
'tuple' object has no attribute 'has_header'
Request Method: GET
Request URL: http://10.6.208.12:8000/declaration/2991/detail
Django Version: 1.4
Exception Type: AttributeError
Exception Value:
'tuple' object has no attribute 'has_header'
Exception Location: /usr/local/lib/python2.6/dist-packages/django/utils/cache.py in patch_vary_headers, line 140
Python Executable: /usr/bin/python
Python Version: 2.6.6
答案 0 :(得分:0)
正确的语法是
<script type="text/javascript">
function initTree(showline)
{
{% for section, secval in the_section_setting.iteritems %}
alert("{{secval.included}}")
{% endfor %}
}
</script>
为了更加详细,我会选择像
这样的东西<script type="text/javascript">
function initTree(showline)
{
{% for section, secval in the_section_setting.iteritems %}
{% if secval.include %}
alert("The {{ section }} is included.")
{% else %}
alert(""The {{ section }} is not included.")
{% endif %}
{% endfor %}
}
</script>
帮助文档 - https://docs.djangoproject.com/en/1.4/topics/templates/