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 %}
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.
{% 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With