Question

On the following image you will see a ranking grid, but you also a column with no values.

This is what the function below does: it calculates the total points per user based on an IDemployee. What I need is to find a way to pass the points to the template on the for loop so that it can be related to each user. I don't know if it's possible to embed a Python function inside a Django template system.

{% for fields in employee %}

   {{fields.name}}
   {{fields.department}}
   {{fields.username}}

    {{ mypoints(fields.id) }} // the total points of user based on my_points() function.

Also looking for a way to order them in DESCENDING ORDER based on points.

Any ideas?

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
Was it helpful?

Solution

You can't directly call the function in django template engine. You can achieve this using django template filters. Try this way,

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

OTHER TIPS

Functions

You can't create Functions within the template because templates are for displaying info and don't handle business logic for the most part. But, you can use greater than, less than, etc.. type of simple evaluators within the template tags. Here is the official documentation for that:

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

Descending order

Use order_by

So for the Employee model to order by Puntos:

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

Then merely add the employee context to your view. Note that the minus sign before puntos means descending order. Without it, it returns ascending.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top