0

Django 版本:1.2.3

我想将 Form 对象从模板标签传递到该标签的专用视图。我的代码如下:

    from django import template
    from django.core.urlresolvers import reverse
    from django.utils.translation import ugettext, gettext_lazy as _
    from django.conf import settings
    from django.utils import simplejson 

    register = template.Library()

    ........

    @register.inclusion_tag('template.html', takes_context=True)
    def asset_diskencryption_settings(context, var1, form):

    context["form"] = form
    context["var1"] = var1

    return '' // Probably I have to use some method which allows me to return some variables to the template

在 template.html 我想使用表单变量,例如 {{form.password}}

4

1 回答 1

2

要设置变量,您应该使用赋值标签

@register.assignment_tag(takes_context=True)
def get_form(context, my_variable):
    form = Form() # do you stuff here
    return form

并像这样使用它:

{% get_form 'my_variable' as form %}
{{ form.password }}
于 2013-06-20T06:58:23.790 回答