1

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!

fstab
  • 4,801
  • 8
  • 34
  • 66

2 Answers2

3

That's not what urlize is for. It's for searching for URLs inside a block of text and converting them to clickable links. The result of the {% url %} doesn't even look like a link in that way, as it's a relative link (eg /index/ rather than www.example.com/index), and the docs are pretty specific about what urlize looks for:

This template tag works on links prefixed with http://, https://, or www..

Not sure why you'd want it anyway: it's trivial to define the link yourself:

This is my link: <a href="{% url 'myview' %}">My view</a>
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Hi, thanks for the hint. Do you know any way to create the link automatically from a view's name? Because that's what I wanted, using the nice automatization of the urlize filter. – fstab Oct 09 '13 at 15:23
2

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>
Kevin Stone
  • 8,831
  • 41
  • 29