Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include with url variable in Django template

I have a "create new object" button in a template file.

I want to include the button 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 from {% url 'create_new_item' %}.

Can I do something like

{% include "snippets/icon_add.html" with link={% url 'create_new_item' %} only %}
like image 507
Jamgreen Avatar asked Apr 15 '15 12:04

Jamgreen


People also ask

How do I reference a URL in Django?

The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py . Listing 2-16 shows how to name a project's home page, as well as how to reference this url from a view method or template.

What does %% include?

{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.

What is include () in Django?

The include tag allows you include a template inside the current template. This is useful when you have a block of content that are the same for many pages.


1 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.

like image 108
Joseph Victor Zammit Avatar answered Oct 09 '22 18:10

Joseph Victor Zammit