Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 %} 
like image 715
grigno Avatar asked Dec 21 '22 04:12

grigno


1 Answers

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.

like image 168
Alasdair Avatar answered Feb 08 '23 20:02

Alasdair