[Answer]-Placing a link with onClick property in django helptext field

1👍

The string is not treated as a Django template, so you can not use the url template tag.

Instead of

{% url 'schools_add' %}

You can use the reverse to get the url

schools_add_url = reverse('schools_add')

then substitute it into your string.

help_texts = {
    'school': mark_safe(
        "<a id='school_add' href = '#' onClick='ModalToggle('%s','%s','#form','Add school'); return false;' >Add</a>" % (schools_add_url, schools_add_url)
    )
}

You should always be careful when using mark_safe for help text. In this case, you are not including any content from the user, so it’s ok.

Leave a comment