Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an url as parameter of include

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!

like image 468
trnsnt Avatar asked Nov 29 '12 15:11

trnsnt


2 Answers

Have you tried this syntax:

{% url 'some-url-name' arg arg2 as the_url %}
{% include "button.html" with title="title" link=the_url %}
like image 90
rayed Avatar answered Oct 04 '22 02:10

rayed


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

like image 36
Brandon Avatar answered Oct 04 '22 03:10

Brandon