在下图中,您将看到一个排名网格,但您也可以看到一个没有值的列。你知道吗
下面的函数就是这样做的:它根据IDemployee
计算每个用户的总分。我需要的是找到一种方法将这些点传递给for
循环上的模板,以便它可以与每个用户相关。我不知道是否有可能在Django模板系统中嵌入Python函数。你知道吗
{% for fields in employee %}
{{fields.name}}
{{fields.department}}
{{fields.username}}
{{ mypoints(fields.id) }} // the total points of user based on my_points() function.
也在寻找一种基于点的DESCENDING ORDER
排序方法。你知道吗
有什么想法吗?你知道吗
def my_points(idemployee):
answer_query = answers.objects.select_related(
'question').filter(empleado=idemployee)
points_total = 0
match = 0
for answer in answer_query:
if answer.question.golesEquipoA == answer.equipoA and answer.question.golesEquipoB == answer.equipoB:
points_total += 4
match += 1
else:
if answer.question.golesEquipoA == answer.question.golesEquipoB and answer.equipoA == answer.equipoB:
points_total += 3
match += 1
else:
if answer.question.golesEquipoA < answer.question.golesEquipoB and answer.equipoA < answer.equipoB:
points_total += 3
match += 1
if answer.question.golesEquipoA > answer.question.golesEquipoB and answer.equipoA > answer.equipoB:
points_total += 3
match += 1
args = {}
args['points'] = points_total
args['match'] = match
return args
功能
您不能在模板中创建
Functions
,因为模板是用于显示信息的,并且在很大程度上不处理业务逻辑。但是,可以使用大于、小于等。。template tags
中简单求值器的类型。以下是官方文件:https://docs.djangoproject.com/en/dev/ref/templates/builtins/
降序
使用
order_by
因此,对于
Employee
模型按Puntos
排序:然后只需将
employee
上下文添加到您的视图中。 注意,在puntos
之前的minus sign
表示descending
顺序。没有它,它返回ascending
。你知道吗不能直接调用django模板引擎中的函数。您可以使用django模板过滤器来实现这一点。这样试试
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters
相关问题 更多 >
编程相关推荐