Agustin Machiavello
Agustin Machiavello

Reputation: 87

How to pass dynamic url to template in django

I want to create a template that receives a 'link_url' variable and I want to pass it a dynamic URL, like this one: {% url "app:genericlistmodel" model="model_handle" %}. This way I could modularize this template and save a lot of time.

The main problem here is that code like this "{% example %}" can not be inside the {% include %} tag, and I don't want to pass hardcoded url's.

Things I've tried that are not working:

{% include 'snippet.html' with link_url={% url "gepian:genericlistmodel" model="entrepreneurs" %} %}

{% url "gepian:genericlistmodel" model="entrepreneurs" %}
{% include 'snippet.html' with link_url=url %}

{% include 'snippet.html' with link_url=url "gepian:genericlistmodel" model="entrepreneurs" %}

Thanks :)

Upvotes: 1

Views: 792

Answers (1)

danish_wani
danish_wani

Reputation: 872

In your template:

{% load custom_filters %}

{% include 'snippet.html' with link_url="entrepreneurs"|get_url %}

templatetags/custom_filters.py

from django import template
from django.urls import reverse
register = template.Library()


@register.filter
def get_url(argument):
    reverse("gepian:genericlistmodel" kwargs={'model': argument})

Upvotes: 1

Related Questions