> You can display custom HTML code for results by setting the data-html attribute on your widget and overriding the view get_result_label() method to return HTML code.Have you tried that ?
from django.utils.html import format_html
class CountryAutocomplete(autocomplete.Select2QuerySetView):
def get_result_label(self, item):
return format_html('<img src="flags/{}.png"> {}', item.name, item.name)
class PersonForm(forms.ModelForm):
class Meta:
widgets = { 'birth_country': autocomplete.ModelSelect2( url='country-autocomplete', attrs={'data-html': True} ) }
If I assume that's a typo and replace autocomplete.ModelSelect2 with CountryAutocomplete it throws an error:
AttributeError: 'CustomisableAutocomplete' object has no attribute 'is_hidden'.
Haven't found anything else on this so I've really got no clue what to do.
autocomplete.ModelSelect2( url='country-autocomplete',
class CountryAutocomplete(autocomplete.Select2QuerySetView):
def get_result_label(self, item):
return format_html('<img src="flags/{}.png"> {}', item.name, item.name)
Through the declaration of CountryAutocomplete as view for url with name "country-autocomplete":
https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#register-the-autocomplete-view