当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - how to get values from views.py and use it in Javascrpit tag

问题描述:

This is my views.py

def list_note(request):

note_info = Note.objects.filter(id_teacher__exact=request.user.id).select_related()

actual_date = date.today()

for notes in note_info:

note_date = notes.remind.date()

tmp = actual_date + timedelta(days=3)

note_expired_list = []

if tmp == note_date:

print()

else:

note_expired_list.append(notes)

print(note_expired_list)

note_data = {

"note_details": note_info,

"note_expired_list_details": note_expired_list,

}

return render_to_response('list_note.html', note_data, context_instance=RequestContext(request))

I want use value note_expired_list_details in <scrpit> tag to display this in alert. How do this?

I try use {{ note_expired_list_details}} but in <script> tag not work.

This is a part of my templates (I try finding HTML element by Id in JS)

{% extends "base.html" %}

{% load staticfiles %}

{% block content %}

<div >

{% for details in note_expired_list_details %}

<p>{{ details }}</p>

{% endfor %}

<script>

var x = document.getElementsByTagName("p");

alert("Test\n" + x[1].childNodes.nodeValue + "\n");

</script>

</div>

{% endblock %}

网友答案:

Why not store value of {{ note_expired_list_details }} in a JS variable and then use it however you want?

<script>
    var expiredList = '{{ note_expired_list_details }}';

    alert(expiredList);
</script>
分享给朋友:
您可能感兴趣的文章:
随机阅读: