這是我views.py
如何從views.py獲取值,並用它在Javascript代碼
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))
我想在<scrpit>
標籤使用價值note_expired_list_details
在alert
進行顯示。這是怎麼回事?
我嘗試使用{{ note_expired_list_details}}
,但在<script>
標記不起作用。
這是我的模板的一部分(我嘗試通過標識在JS找到HTML元素)
{% 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 %}
用你的javascript代碼添加你的模板 –
你正在使用哪個版本的django? – Ali
Django 1.8.4,Python 2.7.9 – user3618304