I would like to know how I can pass url as parameter of an include in django template.
I have in my code, a HTML file that is used to create a button. So when want to add this button I just include it. Problem is that 1 of the parameters is URL, and for now I have not found other solution than putting text url.
SO what I have this:
{% include "button.html" with title="title" link="/new-event/" %}
and I would have to have something like:
{% include "button.html" with title="title" link={% url myview.view%} %}
Thank you very much!
Have you tried this syntax:
{% url 'some-url-name' arg arg2 as the_url %}
{% include "button.html" with title="title" link=the_url %}
I believe you would have to assign the url to a variable that is added to the context in order to use it in an include tag.
Example:
view:
from django.core.urlresolvers import reverse
def your_view(request):
url = reverse('the_url_name_to_reverse', args=[], kwargs={})
return render(request, 'the-template.html', {'url': url})
template:
{% include "button.html" with title="title" link=url %}
If it's a value you need in every template, you might consider adding a context processor to add the reversed url value to the context
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