【问题标题】:Comparing appengine database values in Django比较 Django 中的 appengine 数据库值
【发布时间】:2011-11-11 12:53:58
【问题描述】:

您好,我有一个带有以下 db.Model 的 appengine 应用程序:

class Cinema(db.Model):
    name = db.StringProperty()
    address = db.StringProperty()
    distance = db.IntegerProperty()
    user = db.ReferenceProperty(RunningUser)

当我填写模板时,一切正常:

            {% for cinema in cinemas %}
                <tr>
                    <td><img src="/images/cinema.png"></td>
                    <td>
                        <a href="...">
                            <h2>{{cinema.name}}</h2>
                        </a>
                        {{cinema.address}}
                    </td>
                    <td>
                        {% if cinema.distance > 10000 %}
                            <p>red</p>
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}

if 语句除外。 Python 提出了一个TemplateSyntaxError: 'if' statement improperly formatted exception。根据 Django 应该没问题。那么这三行有什么问题呢?

{% if cinema.distance > 10000 %}
    <p>red</p>
{% endif %}

【问题讨论】:

  • 确保您引用的 django 文档版本与您在 appengine 上使用的版本相同。
  • 感谢您的回答。 0.96 版(这是 appengine 的标准版本)不支持更大的运算符。
  • Appengine 有 django 1.2。您可以使用use_library (stackoverflow.com/questions/4994913/…) 更改版本。虽然我个人认为 django 不适合 appengine。我会推荐 Kay FrameworkFlask 而不是 Django。我认为 Django 只是一个巨大的开销,在 appengine 上使用时会让人头疼。
  • 感谢您提供此信息。我正在将 django 与 appengine 和 python 一起使用。特别是对于我的目的(我只使用模板系统),它不会产生任何开销,并且非常适合我的原型 webapps。

标签: django google-app-engine django-templates


【解决方案1】:

试试这个:

{% if cinema.distance > 10000 %}
    <p>red</p>
{% endif %}

来自:https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#id3

【讨论】:

  • 我尝试了所有带括号的变体......都没有奏效......还是我错过了一个细节?
  • 上述解决方案必须有效!如果您仍然收到错误,它可能在您的模板中的其他地方!仔细检查所有 if 标签!并确认您是否正在关闭块(endif)!
  • 我认为问题在于我使用的是标准 django 版本的 appengine(即 0.96)。这个版本好像不支持 > 操作符。
  • 完全正确!如果您查看 version1.0 文档:docs.djangoproject.com/en/1.0/ref/templates/builtins/… 您可以看到没有 > 运算符!太糟糕了!!这样您就可以将视图中的布尔变量返回到模板!
猜你喜欢
  • 2023-01-31
  • 1970-01-01
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
相关资源
最近更新 更多