[Fixed]-Add a class to dynamically added template element

1👍

You can use a custom template filter to add class to the rendered element.

from django import template

register = template.Library()

@register.filter(is_safe=True)
def class_to_url(value):
    return value.replace('<a ', "<a class='myClass' ">)

And then use it in your template with urlize:

{{ text_url|urlize|class_to_url }}

See django docs for custom template tags and filters

👤v1k45

Leave a comment