How to customize django form label
customization, django, forms, label
Solution
Here's an example of how to add some HTML to form labels:
from django.template.defaultfilters import mark_safe
class MyForm(forms.Form):
my_field = forms.CharField(
max_length=100,
label = mark_safe('<strong>My Bold Field Label</strong>')
)
Problem
``` class permForm(forms.Form): def __init__(self, data=None, **kwargs): super(permForm, self).__init__(data, **kwargs) for item in list(AdminMenu.objects.filter(parent_id=0)): self.fields['menu_%d' % item.id] = forms.BooleanField(label=item.title) for childitem in list(AdminMenu.objects.filter(parent_id=item.id)): arr=[] arr.append(str(item.id)) arr.append(str(childitem.id)) self.fields['menu_%s' % '_'.join(arr)] = forms.BooleanField(label=childitem.title) ``` This will return category: checkbox add category: checkbox List Category:checkbox Food: checkbox Add Fooditems: checkbox List Fooditem: checkbox Tables: checkbox Add Tables: checkbox List Tables: checkbox Users: checkbox View Users: checkbox How can i display it as following category: checkbox add category: checkbox List Category:checkbox Food: checkbox Add Fooditems: checkbox List Fooditem: checkbox Tables: checkbox Add Tables: checkbox List Tables: checkbox Users: checkbox View Users: checkbox I WANT TO MAKE PARENT CATEGORY LABEL BOLD TO DISTINGUISH IT FROM CHILD. POSSIBLE? I DONT WANT TO USE HARD CODED FORMS