Include with url variable in Django template

Question:

I have a "create new object" button in a template file. I want to include the button in several places at my website, but I want to include the template with a link. I’ve tried:

{% include "snippets/icon_add.html" with link="/create_new_item/" only %}

But I want to use the benefits of {% url 'create_new_item' %}. Can I do something like:

{% include "snippets/icon_add.html" with link={% url 'create_new_item' %} only %}
Asked By: Jamgreen

||

Answers:

Try with {% url ... as var %} syntax. Example:

{% url 'create_new_item' as the_url %}
{% include "snippets/icon_add.html" with link=the_url %}

Docs link here.