【问题标题】:Django if tag not evaluating equalityDjango if标签不评估相等性
【发布时间】:2012-09-13 15:17:39
【问题描述】:

我有一个大的 JSON 对象(来自 Google Feed API)和一个来自上下文的 URL。当上下文中的 URL 与提要条目 URL 中的 URL 相同时,django 应该解析!

{for aEntry in feed.entries %}
{% if aEntry.link == {{my_URL}} %}

<p>URL FOUND in feed entry</p>
{% endif %}
{% endfor %}

这不行!!

编辑:

feed: {
            "feedUrl": "http://.blogspot.in//feeds/posts/default",
            "title": "",
            "link": "http://.blogspot.com/",
            "author": "",
            "description": "",
            "type": "atom10",
            "entries": [{
                "title": "Shades",
                "link": "http://.blogspot.com/myurl.html",
                "author": "",
                "publishedDate": "Sun, 29 Jul 2012 04:07:00 -0700",
                "contentSnippet": "abstract art!",
                "content": " HTML CONTENT HERE",
                "categories": ["abstract"]
            }

context = { "feed" : feed, "my_url" : "http://.blogspot.com/myurl.html"}

【问题讨论】:

  • @anhtran 是的,{{my_URL}}
  • @anhtran 哎呀。它在这个问题上的错误.. 更正了!
  • 你能发布来自 Google Feed 的 JSON 模式吗?

标签: python django django-templates django-views


【解决方案1】:
{% if "aEntry.link" == {{my_URL}} %}

需要

{% ifequal aEntry.link my_URL %}

【讨论】:

  • 它不工作。实际上这是我问题中的一个小错字!
  • {{my_URL}} 也需要松开括号
  • 正是——在if语句内部,可以引用上下文变量; {{}} 语法是将它们插入到响应的 HTML 中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2015-02-28
  • 2011-06-19
  • 2012-09-12
  • 2012-11-17
相关资源
最近更新 更多