简体   繁体   中英

Passing DateTime data from Django to Javascript

I am trying to pass data (step_count_data) from Django to a JavaScript function. Here are the code:

Django

#somecode
step_count_date.append(str(step_count_list[i].startTime.date()))

        context = {'step_count_date': json.dumps(step_count_date)}
        return render_to_response('patient-profile.html', context, context_instance=RequestContext(request))

Javascript:

step_from_django = JSON.parse({{ step_count_date }})
console.log(step_from_django);

However I got an error: Uncaught SyntaxError: Unexpected token & and the error line is

step_from_django = JSON.parse(["2015-03-19", "2015-04-02"])

What I want is just the date without the " wrap around. Any idea why and how can I fix this? Thanks

You can do it like this:

step_from_django = JSON.parse({{ step_count_date|safe }});

or simply like this:

step_from_django = {{ step_count_date|safe }};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM