2

I have a list with dates in it.

event_labels = []
for item in all_list:
    event_labels.append(str(item.event_created_at))

context['graph_data_labels'] = json.dumps(event_labels)


"['2018-05-18', '2018-05-17', '2018-05-16', '2018-05-15', '2018-05-14', '2018-05-13', '2018-05-12', '2018-05-11']"

I am using this data to output for labels in chartjs. Problem is the sourrounding double quotes around the event_labels list.

I have another list with just integers and it does not have the sourrounding quotes around the beginning and the end of the list.

[26.0, 50.0, 27.0, 87.0, 46.0, 24.0, 18.0, 34.0]

How do I output a string list without the double quotes at the beginning and end?

In the frontend HTML its like this:

<div>[26.0, 50.0, 27.0, 87.0, 46.0, 24.0, 18.0, 34.0]</div>

<div>"['2018-05-18', '2018-05-17', '2018-05-16', '2018-05-15', '2018-05-14', '2018-05-13', '2018-05-12', '2018-05-11']"</div>

Problem is in the console or if I just print it, I dont see the "

So the output that I want is:

<div>['2018-05-18', '2018-05-17', '2018-05-16', '2018-05-15', '2018-05-14', '2018-05-13', '2018-05-12', '2018-05-11']</div>
Sharpless512
  • 3,062
  • 5
  • 35
  • 60

2 Answers2

4

json.dumps(event_labels) convert list object to the string. So it's adding quotes in the template. You need to remove this line in the view and add to the context list directly:

event_labels = []
for item in all_list:
    event_labels.append(str(item.event_created_at))

context['graph_data_labels'] = event_labels

Also you can use safe template filter to disable escaping:

<div>{{graph_data_labels|safe}}</div>
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
  • Removed json.dumps. Still the same. It seems to only add " when the list is strings, its not added when its integers – Sharpless512 May 19 '18 at 10:01
  • @Sharpless512 probably you overrided later `graph_data_labels`. Can you post fill view code and related tempate part? Also try `
    {{graph_data_labels|safe}}
    ` to render singlequotes in html without escaping.
    – neverwalkaloner May 19 '18 at 10:07
  • 1
    {{graph_data_labels|safe}} worked! Thanks! – Sharpless512 May 19 '18 at 10:08
0

use:-

stringVariable.strip('"')
nandal
  • 2,544
  • 1
  • 18
  • 23