我正在尝试使用变量访问字典中的值,所有这些都在遵循 Django 模板语言的 HTML 文件中。Django 的模板语言不允许您使用变量作为键来访问字典,因此我决定使用 Django 过滤器来执行此操作。但是,我收到“无效过滤器”错误,我不知道为什么。
我的 html 文件
<table class="table">
<tbody>
<tr>
<th scope="row">Username</th>
<th scope="row">Full Name</th>
<th scope="row">Points</th>
{% for subject in subjects %}
<th scope="row">{{ subject }}</th>
{% endfor %}
</tr>
{% for user in users %}
<tr>
<td>
<a href="{% url 'profile' username=user.username %}">{{ user.username }}</a>
</td>
<td>{{ user.first_name }} {{ user.last_name }}</td>
<td>{{ user.profile.points.total }}</td>
{% for subject in subjects %}
<td>{{ user.profile.points|keyvalue:subject }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
Run Code Online (Sandbox Code Playgroud)
我的过滤器.py …