【发布时间】:2015-11-23 15:01:38
【问题描述】:
我有一个页面列出了某个coach 拥有的所有athletes。但是,教练可以拥有多个团队,我试图让他们从页面顶部的下拉列表中选择一个团队,并动态过滤运动员列表以仅显示所选团队中的运动员。
我的模板:
<table class='table'>
<tr>
<td><h3>Team</h3></td>
<td><h3>First Name</h3></td>
<td><h3>Last Name</h3></td>
<td><h3>Email</h3></td>
</tr>
{% for athlete in athletes %}
{% if not athlete.coach_ind %}
<tr><td>
{% for team in athlete.team.all %}
{{ team.school }} {{ team.mascot }} {{ team.sport }}
{% endfor %}
</td>
<td>{{ athlete.user.first_name }}</td>
<td>{{ athlete.user.last_name }}</td>
<td>{{ athlete.user.email }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
我的看法:
teams_list = request.user.profile.team.all()
athletes = UserProfile.objects.filter(team__in=teams_list).order_by('team','user__last_name')
我能够成功获取所有运动员及其信息的正确列表,但我只是不确定如何创建仅按团队显示的动态过滤器。
【问题讨论】:
-
您需要编写一个 ajax 调用来从您的服务器中提取数据并更新列表。 django 模板自然不能做任何事情。
-
执行此操作的流程是什么?
-
我做了一些谷歌搜索,这里有一个例子:devinterface.com/blog/en/2011/02/…,不确定对你是否有意义。