grigno
grigno

Reputation: 3198

django-template assign url to a variable

How can I assign an url to a variable into a template? I've tried:

{% with url_news = url 'news'  %}

and

 {% with url 'news' as url_news %} 

Upvotes: 4

Views: 1924

Answers (1)

Alasdair
Alasdair

Reputation: 309089

Use the as option of the url tag

{% url 'news' as the_url %}
{% if the_url %}
<a href="{{ the_url }}">Link to optional stuff</a>
{% endif %}

Note that when you use this syntax, it will fail silently if the url can't be resolved.

Upvotes: 12

Related Questions