How to use render_to_response to render an html page in a div in Django?

I have a view that returns data to a template using the render_to_response function, as shown 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)) 

Ability to view contextual data in my template (jsComp.html) using the notation below

  // Global variable jdata {% if wc_V_json %} jdata = {{ wc_V_json|safe }}; {% endif %} alert('Sk: '+JSON.stringify(jdata)); 

I wanted to use this template myself inside a div in another template, say index.html. Also, I would like to send the id from index.html via an angularjs ajax call. therefore, my view should take into account that cid is equal to the value that I pass through angular ones.

My question is: how can I do this 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 index.html ?? Could you help me with the approach?

I know that I can use something like this, but it does not work (I tried calling load_page in the success function, but did not use it. 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 below in the view, but it does not work - (Request, Reply)

 @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)) 
0
angularjs django django-views
Jan 14 '16 at 13:53 on
source share
1 answer

This is not a Django question. If you are returning data from your server that you want to insert into your page, you simply do this using the usual jQuery methods:

 .success(function(data) { $('#info').html(data) }) 
0
Jan 14 '16 at 16:06
source share



All Articles