开发者

Django template tag for form elements

I have problem with templatetags usage in Django.Let me define my html and template tag.

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_de开发者_开发百科tail.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] }

My form has fields such as

caption_1
city_1
country_1
caption_2
city_2
country_2

What I want do is grouping caption,country and city by photo id while render those fields.

I try to illustrate my aim at the code above but it doesn't work.

How can I achive this ?

Thanks


You're a little confused about inclusion tags. Inclusion tags don't go inside the template they refer to - they render that template. So the {% getFormElements form photo.pk %} bit belongs in the main template, then the various fields go in the template that's rendered by the tag.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜