I want to convert something like that id,name to something like that "id","name" in django template.
I looked at django template built in tags but none of them works.
Does anyone have an idea to achieve it?
I want to convert something like that id,name to something like that "id","name" in django template.
I looked at django template built in tags but none of them works.
Does anyone have an idea to achieve it?
you can do this by writing a custom Template Tag:
besides your templates folder add a directory named templatetags.
create convert_tags.py in your templatetags directory:
# convert_tags.py
from django import template
register = template.Library()
@register.filter()
def add_quotes(value: str):
""" Add double quotes to a string value """
return f'"{value}"'
In your templates load convert_tags:
<!-- template.html -->
{% load convert_tags %}
{{ your_str|add_quotes }}
You can simply
"{{ id }}", "{{ name }}"
Or you can define a custom template tag to do this dynamically
from django import template
register = template.Library()
@register.simple_tag
def quote(str):
return f'"{str}"'
And in your template
{{ id | quote }}, {{ name | quote }}
Thanks for everyone, I solved it by creating a custom template tag
Here's the code :
from django import template
register = template.Library()
def add_quote(var):
return '"{0}"'.format(var)
@register.filter()
def add_quotes(value):
""" Add double quotes to a string value """
excluded_fields = value.split(',')
return ",".join(add_quote(i) for i in excluded_fields)