Search code examples
djangopython-2.7iconsclickable

Django - Python help_text as a clickable icon


I want to add help text to my forms. However, I want it to appear as a small question mark icon to the right of the input box, when clicked on, will show the help_text.

I have a way of doing it through the html, but when I have more than one field with help_text, its gets buggy.

Main question, anyway of doing this using Django and not the html? If so, what would the steps be to implement?

Thanks


Solution

  • You could create a templatetag, something along these lines should work (untested)

    @register.simple_tag
    def render_help_text(field):
        if hasattr(field, 'help_text'):
            return mark_safe(
                "<a><img src='/static/img/icons/help.gif' title='{help_text}' /></a>".format(**{'help_text': field.help_text})
            )
        return ''
    

    template

    {% for field in form %}
        <!-- other stuff -->
        {% render_help_text field %}
    {% endfor %}