0

I have something like:

{% for mother in mothers_list %}
    {% for father in fathers_list %}
        {% Child.objects.get(mother=mother, father=father) as child %}
            child.name

Unfortunately, I can't call a function with parameters from the template, so this line

{% Child.objects.get(mother=mother, father=father) as child %}

wont work. Any ideas of how how can I get the Child object each time?

3 Answers 3

2

you can write a custom template tags for this and this will be like:

In your project/templatetags/custom_tags.py:

    from django.template import Library
    register = Library()
    @register.filter
    def mother_father(mother_obj, father_obj):
            // Do your logic here
            // return your result 

In template you use template tags like:

{% load custom_tags %}

{% for mother in mothers_list %}
    {% for father in fathers_list %}
        {{ mother|mother_father:father }}
Sign up to request clarification or add additional context in comments.

Comments

0

Read https://docs.djangoproject.com/en/dev/howto/custom-template-tags/ and write a custom tag.

Comments

0

You could do this processing in the view function.

In views.py:

children_list = []
for mother in mothers_list:
    for father in fathers_list:
        try:
            child = Child.objects.get(mother=mother, father=father)
        except Child.DoesNotExist:
            child = None
        children_list.append(child)

Then in your template:

{% for c in children_list %}
    {{ c.name }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.