i have html component that i want to include in my template that has a link as below
<a href="{{url_name}}" ........
in my template i am trying to pass the url_name
using the with
tag
{% include 'components/buttons/_add_button.html' with url_name="{% url 'equipments:equipment-add' %}" reference_name='equipment' %}
this seems not to work as i get the following error
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '"{%' from '"{%'
this works though
using the hardcoded url works however i wanted to implement this using url names for flexibility
{% include 'components/buttons/_add_button.html' with url_name="equipments/equipment/add" reference_name='equipment' %}
urls.py
app_name = "equipments"
....
path("equipment/add/",views.EquipmentCreateView.as_view(), name="equipment-add",
),
...
can i use a custom tag to pass this url name to my component template
CodePudding user response:
I solved this by using a custom filter in Django as:
@register.filter
def get_url(url_name):
return reverse(url_name)
I now pass the url_name
in my template as:
{% include 'components/buttons/_add_button.html' with url_name="equipments:equipment-add" reference_name='equipment' %}
Then in my Html component I use:
<a href="{{url_name|get_url}}"...