pythondjangotemplatesurlurlize

how to use the urlize filter on the templates' url function?


In a Django template, I am trying to use the urlize filter just after the url function is being used, which is generating an url and subsequently generating its <a> tag.

Unfortunately various attempts of using (url myview)|urlize and similar experiments did not work. Any ideas on how to put the pieces together to make the thing work?

thanks!


Solution

  • You need to assign the url to a variable like so:

    {% url "myview" as my_url %}
    {{ my_url|urlize }}
    

    You can also just use {% filter %} to accomplish the same without assigning first:

    {% filter urlize %}{% url "myview" %}{% endfilter %}
    

    With the poster above, it doesn't really make much sense to do any of this. Why wouldn't you just create the hyperlink itself rather than rely on the filter to convert from a url to a hyperlink?

    <a href="{% url "myview" %}">{% url "myview" %}</a>