Custom HTML on items

11 views
Skip to first unread message

kalev....@gmail.com

unread,
Nov 7, 2018, 7:03:45 AM11/7/18
to yourlabs
How to customize the looks of items in autocomplete? There's a paragraph in docs suggesting that it's possible (https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#displaying-results-using-custom-html) but not enough to get going.

Jamesie Pic

unread,
Nov 7, 2018, 8:02:37 AM11/7/18
to yourlabs
> 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 ?

kalev....@gmail.com

unread,
Nov 7, 2018, 10:17:46 AM11/7/18
to yourlabs

On Wednesday, November 7, 2018 at 1:02:37 PM UTC, Jamesie Pic wrote:
> 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 ?

That's what I was referring to. There are two classes in the paragraph that don't seem to be connected, CountryAutocomplete is not used in PersonForm.

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.

Jamesie Pic

unread,
Nov 7, 2018, 10:25:44 AM11/7/18
to yourlabs
Actually, the classes are conected through the url:

   autocomplete.ModelSelect2( url='country-autocomplete',

Connects to:
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

Jamesie Pic

unread,
Nov 7, 2018, 10:29:31 AM11/7/18
to yourlabs
Oh, I think I see the confusion.

Maybe you are expecting to have the same rendering both from the autocomplete view and the initial widget rendering.

In this case, you will need to define a rendering function and use it in both your view, and widget. In v2 we had a way, but the software became bloated and too expensive to maintain.

That as a builtin feature, is under ongoing development at very early stage: https://github.com/yourlabs/django-autocomplete-light/issues/1067

Does that make sense to you ?

kalev....@gmail.com

unread,
Nov 7, 2018, 11:00:27 AM11/7/18
to yourlabs
Looks interesting.

But the url bit was all that was missing, fixed my problem now. Thank you for your help.
Reply all
Reply to author
Forward
0 new messages