如何在模板中使用函数?

2024-06-23 20:08:59 发布

您现在位置:Python中文网/ 问答频道 /正文

在下图中,您将看到一个排名网格,但您也可以看到一个没有值的列。你知道吗

下面的函数就是这样做的:它根据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排序方法。你知道吗

有什么想法吗?你知道吗

Table view

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

Tags: and函数answerfieldsforifmatchargs
2条回答

功能

您不能在模板中创建Functions,因为模板是用于显示信息的,并且在很大程度上不处理业务逻辑。但是,可以使用大于、小于等。。template tags中简单求值器的类型。以下是官方文件:

https://docs.djangoproject.com/en/dev/ref/templates/builtins/

降序

使用order_by

因此,对于Employee模型按Puntos排序:

employee = Employee.objects.order_by('-puntos')

然后只需将employee上下文添加到您的视图中。 注意,在puntos之前的minus sign表示descending顺序。没有它,它返回ascending。你知道吗

不能直接调用django模板引擎中的函数。您可以使用django模板过滤器来实现这一点。这样试试

from django import template

register = template.Library()

@register.filter
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

https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters

相关问题 更多 >

    热门问题