【问题标题】:checking if element is in list in gae template检查元素是否在 gae 模板的列表中
【发布时间】:2012-01-23 14:15:47
【问题描述】:

我有课:

class Article(db.Model):
     visitedBy = db.ListProperty(int)

context = {
    "article": Article(),
    "id": 3
}

在模板中我尝试这样做:

{% if id in article.visitedBy %}
     <p>Eureka</p>
{% endif %}

但我得到了错误:

TemplateSyntaxError: 'if' statement improperly formatted

我的问题是如果某些项目在列表中,我如何签入模板?

【问题讨论】:

  • 您使用的是哪个django 版本?
  • 在哪里可以查看?我从from google.appengine.ext.webapp import template获得的模板
  • 您在app.yaml 中使用的是runtime: python 还是runtime: python27
  • if id in article 看起来很奇怪,你是说if id in article.visitedBy 吗?
  • python 2.5 运行时的默认 django 版本是 0.96,不支持这种语法。有几个 SO 问题会告诉您如何切换到较新的版本。

标签: python google-app-engine templates


【解决方案1】:

问题是 django 的旧版本。使用本教程

http://code.google.com/intl/pl/appengine/docs/python/gettingstartedpython27/

我将版本更改为新版本,现在效果很好。

Olny 的问题是我没有 jinja2 库。它可以使用此链接安装:

http://jinja.pocoo.org/docs/intro/

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2011-11-20
    • 2014-07-31
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    相关资源
    最近更新 更多