1. 后台的本质是服务器和前端的数据交互 参考资料
2. 最简单的后台
from django.shortcuts import render
def main_page(request):
return render(request, 'index.html')
3. views->html
----views.py----
from django.shortcuts import render
def main_page(request):
data = [1,2,3,4]
return render(request, 'index.html', {'data': data})
----html----
<div>{{ data }}</div>
{% for item in data%}
<p>{{ item }}</p>
{% endfor %}
4. view-> JavaScript
----views.py----
# -*- coding: utf-8 -*-import json
from django.shortcuts import render
def main_page(request):
list = ['view', 'Json', 'JS']
return render(request, 'index.html', {
'List': json.dumps(list),
})
----js----
var List = {{ List|safe }};
5. JavaScript Ajax 动态刷新页面
----views----
def scene_update_view(request):
if request.method == "POST":
name = request.POST.get('name')
status = 0
result = "Error!"
return HttpResponse(json.dumps({
"status": status,
"result": result
}))
----js----
function getSceneId(scece_name, td) {
var post_data = {
"name": scece_name,
};
$.ajax({
url: {% url 'scene_update_url' %},
type: "POST",
data: post_data,
success: function (data) {
data = JSON.parse(data);
if (data["status"] == 1) {
setSceneTd(data["result"], scece_name, td);
} else {
alert(data["result"]);
}
}
});
}
网友评论