【发布时间】:2016-04-19 21:13:40
【问题描述】:
我有一个使用如下 render_to_response 函数将数据返回到模板的视图 -
@page_template("app/jsComp.html") # just add this decorator
def jsComp(request, template="app/jsComp.html", extra_context=None):
basic_v_obj = js_basicinfo.objects.get(cid = request.session.get('id'))
# Sends json data objects in context container
.....
context = {
'cars': basic_v_obj,
'companies':wc_v_obj,
}
context['wc_V_json'] = mark_safe(json.dumps(wc_v_json_list, ensure_ascii=False))
return render_to_response(template, context, context_instance=RequestContext(request))
能够使用以下符号查看我的模板 (jsComp.html) 中的上下文数据
// Global variable jdata
{% if wc_V_json %}
jdata = {{ wc_V_json|safe }};
{% endif %}
alert('Sk: '+JSON.stringify(jdata));
我想在另一个模板的 div 中使用这个模板本身,比如 index.html 另外,我想通过 angularjs ajax 调用从 index.html 发送 id。 所以,我的观点应该认为 cid 等于我通过 angularjs 传递的值。
我的问题是如何在 index.html 中做到这一点?
$scope.preview_ang = function (clicked_id) {
$http({
method: 'POST',
url: 'pvcan',
data: {
'id': clicked_id
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
.success(function (data) {
if (data == "null") {
alert('server returned nothing but success');
} else {
/// WHAT SHOULD I WRITE HERE SUCH THAT I CAN RENDER jsComp.html IN A DIV IN THE INDEX.HTML
}
})
.error(function (data, status, headers, config) {
alert('server returned error :'+status);
})
}
我应该在我的 angularjs ajax 调用的成功函数中写什么以在 index.html 中有一些 id 的 div 中呈现 jsComp.html ?你能帮我解决这个问题吗?
我知道我可以使用这样的东西,但这不起作用(尝试在成功函数中调用 load_page 但没有用。我怎样才能达到这个要求?
function load_page(){
document.getElementById("info").innerHTML='<object type="text/html" data="previewcan" style="width:100%;height:100%;"></object>';
}
在视图中尝试了以下但它不起作用 - (请求,响应)
@page_template("app/jsComp.html") # just add this decorator
def jsComp(request, template="app/jsComp.html", extra_context=None):
data=json.loads(request.body.decode())
v_cid=data["id"]
basic_v_obj = js_basicinfo.objects.get(cid = v_cid)
# Sends json data objects in context container
.....
context = {
'cars': basic_v_obj,
'companies':wc_v_obj,
}
context['wc_V_json'] = mark_safe(json.dumps(wc_v_json_list, ensure_ascii=False))
return HttpResponse(context, context_instance=RequestContext(request))
【问题讨论】:
-
所以你想对你的 Django API 进行一个
POST调用,从那里获取一些id和额外的数据,然后用那个id渲染一个div,它又包含额外的数据。我说对了吗?
标签: angularjs django django-views