【问题标题】:Alias attribute in a django templateDjango 模板中的别名属性
【发布时间】:2017-04-07 20:24:46
【问题描述】:

我们可以为 django 模板中的属性创建别名吗?我认为这行不通,但我尝试了{% loan.customer as customer %}

谢谢!

【问题讨论】:

  • 我认为你用错误的语言标记了这个。
  • @guest 是的,你是对的。

标签: django templates variables alias


【解决方案1】:

您可能需要 with 模板标签。

{% with loan.customer as customer %}     
    <div>{{customer}}</div>
{% endwith %}

这是旧样式,但仍受支持,请参阅 django documentation 了解新风格和更多信息。

目前,新风格

{% with customer=loan.customer %}

【讨论】:

  • 如果我想为许多别名做这样的事情,你能告诉我该怎么做吗?例如,{% loan.customer as customer %}{% request.user as user%}?
  • 我认为实际上嵌套 with 不受 django 模板支持
  • 记住@J.Doe 这是仍然支持的旧样式。我给出了这个答案,因为你的问题是这种格式。现在和新样式将是 {% with customer=loan.customer %}
猜你喜欢
  • 2012-12-24
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
  • 2019-09-24
  • 2011-01-18
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
相关资源
最近更新 更多