Include with url variable in Django template
Asked Answered
I

1

8

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 %}
Interpenetrate answered 15/4, 2015 at 12:26 Comment(0)
S
26

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.

Speedboat answered 15/4, 2015 at 12:32 Comment(1)
With the catch that {% url 'create_new_item' as the_url %} does not thrown an error if the url doesn't exist.Winograd

© 2022 - 2024 — McMap. All rights reserved.