Skip to content

default AutocompleteModelTemplate choice_template renders with extra spaces #318

@aogier

Description

@aogier

(maybe related to #255)

Hi, I've got some minor problem using templated classes.

When registering an AutocompleteModelTemplate eg:

autocomplete_light.register(LocalUser,
                            autocomplete_light.AutocompleteModelTemplate,
                            # Just like in ModelAdmin.search_fields
                            search_fields=['^givenName', 'sn', 'uid', 'displayName'],
                            # This will actually html attribute data-placeholder which will set
                            # javascript attribute widget.autocomplete.placeholder.
                            autocomplete_js_attributes={'minimum_characters': 1,
                                                        },
                            )

html response from /autocomplete/ url will be:

<span class="div" data-value="ppiselli">
    Piero Piselli







</span>

the quickest fix is to override template with something similar:

{% load i18n l10n %}
{% load static %}

{% spaceless %}
<span class="div">
    <span data-value="{{ choice.pk|unlocalize }}">{{ choice }}</span>
    {% with choice.get_absolute_url as url %}
        {% if url %}
        <a href="{{ url }}" target="_blank" class="choiceDetail">&rarr;</a>
        {% endif %}
    {% endwith %}

    {% with choice.get_absolute_update_url  as url %}
        {% if url %}
        <a href="{{ url }}" target="_blank" class="choiceUpdate">
            <img src="{% static 'admin/img/icon_changelink.gif' %}" />
        </a>
        {% endif %}
    {% endwith %}
</span>
{% endspaceless %}

spaceless will do the rest, producing a cleaner:

<span class="div"><span data-value="ppiselli">Piero Piselli</span></span>

This produce better output while maintaining good code readability, and does not break existing javascript

Many thanks for this package, it's great !

ciao,
ale

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions