python - 为什么在这里提出 django TemplateSyntaxError ?
问题描述
我试图遍历我的视图的上下文并仅获取 'ga:deviceCategory' 值,我在 HTML 中运行的代码是:
<div class="card-body">
{% for i in pvdc %}
<p>{{ i.get('ga:deviceCategory') }}</p>
{% endfor %}
</div>
我对 django 的看法是:
def dashboard(request):
context = {'pvdc': [{'ga:deviceCategory': 'desktop', 'ga:pageviews': 673, 'ga:avgSessionDuration': 53.4447946},
{'ga:deviceCategory': 'mobile', 'ga:pageviews': 2373, 'ga:avgSessionDuration': 69.62674418604651},
{'ga:deviceCategory': 'tablet', 'ga:pageviews': 322, 'ga:avgSessionDuration': 26.205426356589147}]}
return render(request, 'user/index.html', context)
我收到的语法错误是:无法解析剩余部分:来自 'i.get('ga:deviceCategory')' 的 '('ga:deviceCategory')'
这里有什么问题,我怎样才能得到这些值?
解决方案
数据不是从 for 循环中呈现的,因此您需要从中删除 get
{{ i.get('ga:deviceCategory') }}
<div class="card-body">
{% for i in pvdc %}
<p>{{ i.'ga:deviceCategory'}}</p>
{% endfor %}
</div>
推荐阅读
- python - 制作 DJANGO 注册页面的最简单方法是什么?
- javascript - Three.js 方法将网格返回为未定义
- python - Pandas:用无替换列中所有小写字符串
- laravel - Laravel - Eloquent 合并集合与单个项目
- python - 为什么 pandas.series.map 的速度如此之慢?
- android - 如何在android中十秒后禁用按钮?
- android - 从firebase数据库检索数据到getvalue中的listview null
- angular - 实例化 Angular 拦截器
- meteor - 在捆绑的流星应用程序上安装 nmp 失败
- three.js - 三.js OrbitControls。如何更新旋转相机?