2011-07-06 62 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