[Solved]-How to pass an url as parameter of include

8πŸ‘

βœ…

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

21πŸ‘

Have you tried this syntax:

{% url 'some-url-name' arg arg2 as the_url %}
{% include "button.html" with title="title" link=the_url %}
πŸ‘€rayed

3πŸ‘

Some might find this solution more suitable:

first create a filter

from django.core.urlresolvers import reverse
@register.filter()
def rev(value):
    return reverse(value)

then use it in the template like this

{% include "button.html" with title="title" link="myview.view"|rev %}
πŸ‘€omar

Leave a comment