Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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
Python 使用带空格的键_Python_Django_Google App Engine_Dictionary_Django Templates - Fatal编程技术网

Python 使用带空格的键

Python 使用带空格的键,python,django,google-app-engine,dictionary,django-templates,Python,Django,Google App Engine,Dictionary,Django Templates,在Django模板中是否有类似于以下内容的方法 {% for hop in hops%} <tr> <td>{{ hop.name }}</td> <td>{{ hop.mass }}</td> <td>{{ hop."boil time" }}</td> </tr> {% endfor %} {%用于跃点中的跃点%} {{hop

在Django模板中是否有类似于以下内容的方法

   {% for hop in hops%}
    <tr>  
      <td>{{ hop.name }}</td>
      <td>{{ hop.mass }}</td>  
      <td>{{ hop."boil time" }}</td>
    </tr>
  {% endfor %}
{%用于跃点中的跃点%}
{{hop.name}
{{hop.mass}
{{hop.“煮沸时间”}
{%endfor%}

啤酒花,“煮沸时间”不起作用。简单的解决方案是重命名关键的沸腾时间,但我对替代方案感兴趣

最好的方法是将属性名潜入另一个变量中,如下所示:

{% for key, value in hop.items %}
    {% ifequal key 'boil time' %}
        {{ value }}
    {% endifequal %}
{% endfor %}
在Django 0.96(Google AppEngine使用的版本)中,模板语言不支持元组扩展,因此有点难看:

{% for hop in hops %}
    <tr>
        <td>{{ hop.name }}</td>
        <td>{{ hop.mass }}</td>
        <td>
            {% for item in hop.items %}
                {% ifequal item.0 'boil time' %}
                    {{ item.1 }}
                {% endifequal %}
            {% endfor %}
        </td>
    </tr>
{% endfor %}

您可以使用来自djangosnippets的
get
过滤器:


(重命名密钥可能更好…

对于django 0.96,这是Google Appengine用于模板的功能,以下功能有效:

{% for hop in recipe.get_hops %}
    {% for item in hop.items %}
          {% ifequal item.0 'boil time' %}
              <p>{{ item.1 }}</p>
          {% endifequal %}
        {% endfor %}   
  {% endfor %}
{recipe.get_hops%}
{hop.items%中的项的%s}
{%ifequal项。0'沸腾时间'%}
{{item.1}}

{%endifequal%} {%endfor%} {%endfor%}

item.0是键,item.1是值

Django模板语言不允许这样做。
hop['boil-time']
的模板语言等价物是
hop.boil-time
(我想这就是他要解决的问题)。下面是我在上面所做评论的参考,说明
符号包含索引。。。我很感激为编写所有代码所做的努力,但由于某种原因,它似乎不适合我,带有复制和粘贴的事件。我无法通过字典进行迭代来打印任何键或值,即使没有ifequals。Dan,我实际上在我运行的实例上测试了代码,在这两种情况下都有效。。。我唯一能想到的就是物品的问题。试着做一个{hop.items}}来看看结果如何。如果字典覆盖了items方法,这可能会导致问题(例如,hop有一个'items'属性,然后尝试hop.iteritems),通过在main中指定版本,实际上可以在Google App Engine上使用Django 1.0(或1.1)。py:我会试一试,让你知道它是如何运行的。老兄,我做了太多Ruby了。我倾向于使用monkeypatch
dict
将下划线转换为空格……我可能应该提到我在google appengine中使用的是django模板,而不是完整的django应用程序。似乎没有一种简单的方法可以添加过滤器。
{% for hop in hops %}
    <tr>
        <td>{{ hop.name }}</td>
        <td>{{ hop.mass }}</td>
        <td>
            {% for item in hop.items %}
                {% ifequal item.0 'boil time' %}
                    {{ item.1 }}
                {% endifequal %}
            {% endfor %}
        </td>
    </tr>
{% endfor %}
{% regroup hop.items by 'boil time' as bt %}
    {% for item in bt %}
        {% if forloop.first %}
            {% for item2 in item.list %}
                {% for item3 in item2 %}
                    {% if not forloop.first %}
                        {{ item3 }}
                    {% endif %}
                {% endfor %}
            {% endfor %}
        {% endif %}
{% endfor %}
{% for hop in recipe.get_hops %}
    {% for item in hop.items %}
          {% ifequal item.0 'boil time' %}
              <p>{{ item.1 }}</p>
          {% endifequal %}
        {% endfor %}   
  {% endfor %}