4

When I add a parathesis to the following label string it's printed in my HTML with a colon appended to it:

question_3 = forms.MultipleChoiceField(choices=QUESTION_3_CHOICES,
widget=forms.CheckboxSelectMultiple(), label = mark_safe('Which of 
these styles do you like? (choose multiple)'))

The label in my form gets outputted like this in my HTML:

 Which of these styles do you like? (choose multiple):

When I remove '(choose multiple)', it prints correctly like this with no colon appended:

Which of these styles do you like?

I tried converting all the text to unicode before using mark_safe by doing this:

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'))

But that didn't fix it...

1 Answer 1

2

If the question is "Why this happens?" then have a look at the source of BaseForm class (at django.forms.forms.BaseForm):

if self.label_suffix:
    if label[-1] not in ':?.!':
        label += self.label_suffix
label = bf.label_tag(label) or ''

By default the BaseForm constructor sets label_suffix=':'.

How to fix? Try passing label_suffix when initializing your form, most likely it inherits from BaseForm.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.