【问题标题】:How to check type of variable in Django template如何检查Django模板中的变量类型
【发布时间】:2021-01-18 17:23:49
【问题描述】:

以下是我的示例代码。我需要知道weather 变量的类型,它可能是List、Dictionary 或Tuple。有没有像我们在python中使用type()这样的简单方法?

{% extends 'base.html' %}
{% block title%}
Home
{% endblock %}
{% block content %}
<h3 class="text-center text-success">Weather Forecast!!</h3><hr>
{{weather}}
<hr>
{% for w in weather%}
{{w}}<br/>
{% endfor%}
{% endblock %}

【问题讨论】:

标签: python-3.x django django-templates


【解决方案1】:

当向模板发送变量时,使用 具体描述如 {{weather.sun}} 或 {{weather.cloudy}}

【讨论】:

  • 但我无法控制“天气”变量。我正在使用 python 请求模块从 URL 读取数据。有时是列表的字典,有时是字典的字典,等等。
  • 如果是这种情况,您只能在视图中使用 type()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-25
  • 2012-08-14
  • 2020-12-19
  • 2017-06-28
  • 2012-10-07
相关资源
最近更新 更多