2011-07-06 65 views
0

我在Django中有模板标签用法问题。让我定义我的html和模板标签。表单元素的Django模板标签

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 

我想要做的是分组caption,country and city通过照片ID,而渲染这些领域。

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

我该如何实现这一目标?

谢谢

回答

2

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

+0

是的你是对的谢谢! – brsbilgic