简体   繁体   English

Django 1.2:自定义表单字段?

[英]Django 1.2: Custom form field?

I've got a dynamic form that contains either one or several MultipleChoiceFields or ChoiceFields. 我有一个动态表单,其中包含一个或多个MultipleChoiceFields或ChoiceFields。 I want to display an instruction to the user, eg for ChoiceField: "Select one of the following", and for MultipleChoiceField: "Select any of the following" 我想向用户显示一条指令,例如ChoiceField:“选择以下内容之一”,MultipleChoiceField:“选择以下内容之一”

How can I do this? 我怎样才能做到这一点? I tried subclassing each of these fields, but I couldn't get the value back out in the template. 我尝试对每个字段进行子类化,但无法在模板中获取该值。

Thanks 谢谢

EDIT 编辑

I tried something like: 我尝试了类似的东西:

class MultiWithInstruction(forms.MultipleChoiceField):
    def __init__(self, instruction=None, **kwargs):
        self.instruction=instruction
        return super(MultiWithInstruction, self).__init__(**kwargs)

I couldn't retrieve the value of 'instruction' in the template. 我无法在模板中检索“指令”的值。

Why not just use help_text ? 为什么不只使用help_text

class MyForm(forms.Form):
    my_field = forms.MultipleChoiceField(help_text='Pick one of these', ....)

And then in the template you could do something like this: 然后,您可以在模板中执行以下操作:

<p>{{ field.label_tag }}: {{ field }}</p>
{% if field.help_text %}<p class="help_text">{{ field.help_text|safe }}</p>{% endif %}

您可以在表单字段中设置标签值:

myfield = forms.MultipleChoiceField(label='Select any of the following')

I had the same problem. 我有同样的问题。 I couldn't find an easy way (without overriding a lot of stuff from django.forms) so I came up with this quick-and-dirty solution. 我找不到一种简单的方法(没有覆盖django.forms的很多内容),所以我想出了这个快速而又肮脏的解决方案。

Define a new template filter, that splits a string into a list, given a separator; 定义一个新的模板过滤器,该过滤器在给定分隔符的情况下将字符串分成列表; see this simple snippet by Ciantic. 参见Ciantic的简单片段 Save the snippet as templatetags/whatever_name.py . 将代码段另存为templatetags/whatever_name.py

In forms.py , fill the help_text attribute of the field with your help and instruction strings, separated by '#' (you can choose whatever separator you want, of course); forms.py ,用帮助和说明字符串填充字段的help_text属性,并用'#'分隔(当然,您可以选择所需的任何分隔符); something like 就像是

my_field = forms.MultipleChoiceField(help_text = '%s#%s' % (help_string, instruction_string), ...)

help_text is a string (already marked safe), so you can't put a list in it (this is why the custom split filter is needed). help_text是一个字符串(已经标记为安全),因此您不能在其中放入列表(这就是为什么需要自定义拆分过滤器的原因)。

And this is an example of a template that shows help and instruction strings for each field in the form: 这是一个模板示例,以表格的形式显示每个字段的帮助和说明字符串:

{% load whatever_name %}

{% for field in form %}
    help: {% filter split:"#"|first %}{{ field.help_text }}{% endfilter %}
    instruction: {% filter split:"#"|last %}{{ field.help_text }}{% endfilter %}
{% endfor %}

Obviously, you can't use as_p , as_table and as_ul to render the form. 显然,您不能使用as_pas_tableas_ul来呈现表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM