问题
I have a view which returns data to template using render_to_response function as below -
@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))
Am able to view the context data in my template (jsComp.html) using below notation
// Global variable jdata
{% if wc_V_json %}
jdata = {{ wc_V_json|safe }};
{% endif %}
alert('Sk: '+JSON.stringify(jdata));
I wanted to use this template itself inside a div in another template say index.html Also, I would like to send id from index.html via angularjs ajax call. so, my view should consider cid equals the value that I pass via angularjs.
My question is how can I do it in 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);
})
}
What should I write in the success function of my angularjs ajax call to render jsComp.html in a div with some id in the index.html ?? Could you please help me with the approach ?
Am aware that I can use something like this, but this is not working (Tried calling load_page in success function but no use. How can I achieve this requirement?
function load_page(){
document.getElementById("info").innerHTML='<object type="text/html" data="previewcan" style="width:100%;height:100%;"></object>';
}
Tried the below in the view but it doesn't work - (Request, Response)
@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))
回答1:
This isn't really a Django question. If you're returning data from your server which you want to insert into your page, you just do that with normal jQuery methods:
.success(function(data) {
$('#info').html(data)
})
来源:https://stackoverflow.com/questions/34791375/how-to-use-render-to-response-to-render-a-html-page-within-div-in-django