Django表单元素的模板标记

时间:2011-07-06 09:20:41

标签: django django-forms django-templates

我在Django中使用templatetags时遇到问题。让我来定义我的html和模板标签。

photo_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对caption,country and city进行分组。

我试图说明我对上述代码的目标,但它不起作用。

我怎么能做到这一点?

由于

1 个答案:

答案 0 :(得分:2)

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