0

I am trying to print comma separated key values after drilling into a dictionary. But I am unable to get rid of dict_keys() that shows in my output.

Here is my template:

metrics.html

<h1>Accessing values</h1>
<p>{{ final_metrics.values }} </p>
<p>{{final_metrics.data.0.13.browser.Chrome}}</p>

<h1>{{ final_metrics.month }} Clicks Statistics</h1>
<p>Short Url: {{final_metrics.short_url }}</p>
<p>Date Accessed: {{ final_metrics.data.0.keys }}</p>  
<p>Browser: {{final_metrics.data.0.13.browser.keys}}</p>
<p>Platform: {{final_metrics.data.0.13.platform.keys}}</p>

and on the output screen it shows as follows

output

Is there a way to get rid of the dict_keys() and have those values in a comma separated format?

I have tried the solutions from https://stackoverflow.com/a/8000091 and https://stackoverflow.com/a/34542858 but does not seem to work for me.

shuberman
  • 1,416
  • 6
  • 21
  • 38
  • It's gross, but this will always work: `{% for i in final_metrics.data.0.keys %}{{i}}{% if not forloop.last %},{% endif %}{% endfor %}` – Nealium Oct 17 '22 at 01:25

1 Answers1

1

I'll throw it in as an answer, but there might be a prettier way of doing it

base

{% for i in dictionary.keys %}{{i}}{% if not forloop.last %},{% endif %}{% endfor %}

full

<h1>Accessing values</h1>
<p>{{ final_metrics.values }} </p>
<p>{{final_metrics.data.0.13.browser.Chrome}}</p>

<h1>{{ final_metrics.month }} Clicks Statistics</h1>
<p>Short Url: {{final_metrics.short_url }}</p>
<p>Date Accessed: {% for i in final_metrics.data.0.keys %}{{i}}{% if not forloop.last %},{% endif %}{% endfor %}</p>  
<p>Browser: {% for i in final_metrics.data.0.13.browser.keys %}{{i}}{% if not forloop.last %},{% endif %}{% endfor %}</p>
<p>Platform: {% for i in final_metrics.data.0.13.platform.keys %}{{i}}{% if not forloop.last %},{% endif %}{% endfor %}</p>
Nealium
  • 2,025
  • 1
  • 7
  • 9