我在模板中有这个代码,我想打印出每个选项得到的票数.投票只是字典而选择是模型对象.
{% for choice in choices %}
{{choice.choice}} - {{votes[choice.id]}} <br />
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
它引发了一条异常,显示"无法解析余数"
在Django模板中,有没有办法从一个有空格的键中获取值?例如,如果我有一个像这样的字典:
{"Restaurant Name": Foo}
Run Code Online (Sandbox Code Playgroud)
如何在模板中引用该值?伪语法可能是:
{{ entry['Restaurant Name'] }}
Run Code Online (Sandbox Code Playgroud) 我有一个错误: 'run_target'不是有效的标记库:找不到模板库run_target,尝试了django.templatetags.run_target
我不知道为什么它不能工作,即使我添加'db.templatefilters'它也无法正常工作......任何人都可以帮助我吗?谢谢 :)
以下是我的文件结构:
db/
models.py
templatefilters/
__init__.py
run_target.py
templates/
run.html
Run Code Online (Sandbox Code Playgroud)
run_target.py
from django import template
register = template.Library()
@register.simple_tag
def dictKeyLookup(the_dict, key):
return the_dict[key]
Run Code Online (Sandbox Code Playgroud)
run.html
{% extends "index.html" %}
**{% load run_target %}**
{% block content %}
<div style="margin-left:150px; margin-top:10px">
<a href="/home">Home</a> >> <b>run</b>
</div>
<form name="form" method="post">
<br>
<input type="submit" value="Delete" style="margin-left:149px; width:80px; height:30px">
<table border="1"; style="margin-left:150px; border-collapse:collapse;margin-top:10px"; cellpadding="4" borderColor=black>
{% for run in run_list %}
<tr>
<td>{% dictKeyLookup target_dict run.id %}</td>
</tr>
{% endfor %}
</table> …Run Code Online (Sandbox Code Playgroud)