表单元素的Django模板标记

表单元素的Django模板标记,django,django-forms,django-templates,Django,Django Forms,Django Templates,我在Django中使用templatetags时遇到问题。让我定义我的html和template标记 photo\u detail.html {% for photo in pList %} {% getFormElements form photo.pk %} {{ caption_field }} {{ city_field }} {%endfor %} photohelper.py from django import template register =

我在Django中使用templatetags时遇到问题。让我定义我的html和template标记

photo\u detail.html

{% for photo in pList %}
    {% getFormElements form photo.pk %}

    {{ caption_field }}
    {{ city_field }}

{%endfor %}
photohelper.py

from django import template

register = template.Library()

@register.inclusion_tag('wpphotos/post/photo_detail.html')
def getFormElements(form,pid):
    return {'caption_field':form.fields['caption_%s' % pid],'country_field':form.fields['country_%s' % pid],'city_field':form.fields['city_%s' % pid] }
我的表单有如下字段

caption_1
city_1
country_1
caption_2
city_2
country_2
我要做的是在渲染这些字段时,按照片id对标题、国家和城市进行分组

我试图用上面的代码来说明我的目标,但它不起作用

我怎样才能做到这一点


谢谢

您对包含标签有点困惑。包含标记不进入它们所引用的模板内部-它们呈现该模板。因此,
{%getFormElements form photo.pk%}
位属于主模板,然后各个字段进入由标记呈现的模板中