当我在下面的标签字符串中添加一个parathesis时,它会在我的HTML中打印,并附加一个冒号:
question_3 = forms.MultipleChoiceField(choices=QUESTION_3_CHOICES,
widget=forms.CheckboxSelectMultiple(), label = mark_safe('Which of
these styles do you like? (choose multiple)'))
我的表单中的标签在我的HTML中输出如下:
Which of these styles do you like? (choose multiple):
当我删除'(选择多个)'时,它会像这样正确打印而不添加冒号:
Which of these styles do you like?
我尝试在使用mark_safe之前将所有文本转换为unicode:
question_3 = forms.MultipleChoiceField(choices=QUESTION_3_CHOICES,
widget=forms.CheckboxSelectMultiple(), label = mark_safe('Which of
these styles do you like? (choose multiple)').decode('unicode-escape'))
但这并没有解决它......
答案 0 :(得分:2)
如果问题是“为什么会这样?”然后看一下BaseForm类的源代码(在django.forms.forms.BaseForm中):
if self.label_suffix:
if label[-1] not in ':?.!':
label += self.label_suffix
label = bf.label_tag(label) or ''
默认情况下,BaseForm构造函数设置label_suffix =':'。
如何解决?尝试在初始化表单时传递label_suffix,很可能它继承自BaseForm。