在Django / oTree中动态更改表单标签

时间:2019-09-10 20:38:40

标签: python django

我有一个要具有可变内容的表单标签。我向模板公开了一个名为outgroup的变量,希望将其包含在formfield标签中。我当前的(错误的)尝试如下所示:

 {% formfield sent_amount label="How much do you want to send to a "+{{outgroup}} %}

但这显然不起作用。将变量放入标签方法的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

为什么您不能在视图中执行类似的操作?

def study(request, studyID):
    if request.method == 'GET' and request.user.is_authenticated:
        form = ContactForm()
        form.fields['from_email'].label = "{}, what's your email?".format(.get_full_name())

使用form.fields["your_label_id"]进行设置。

答案 1 :(得分:0)

当要插入标签中的动态内容是字符串而不是整数时,只需在引用动态变量(在下面的示例中,在self.player.type之后)后添加()。像这样:

首先转到pages.py:

    class Contribute(Page):
        form_model = 'player'
        form_fields = ['type']

        def vars_for_template(self):
            return dict(
                contribution_label='How many {} do you want to contribute?'.format(self.player.type())
            )

SECOND转到相关的HTML页面Contribute.html

    {% formfield player.contribution label=contribution_label %}