0

私は次のようなものを持っています:

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

残念ながら、テンプレートからパラメーターを使用して関数を呼び出すことはできないため、この行

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

動作しません。毎回 Child オブジェクトを取得する方法についてのアイデアはありますか?

4

3 に答える 3

2

このためのカスタム テンプレート タグを記述することができます。これは次のようになります。

あなたの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 

テンプレートでは、次のようなテンプレート タグを使用します。

{% load custom_tags %}

{% for mother in mothers_list %}
    {% for father in fathers_list %}
        {{ mother|mother_father:father }}
于 2011-08-29T00:42:32.410 に答える
0

https://docs.djangoproject.com/en/dev/howto/custom-template-tags/を読み、カスタムタグを記述します。

于 2011-08-28T22:50:45.170 に答える