How to use render_to_response to render a html page within div in Django? [duplicate]

情到浓时终转凉″ 提交于 2019-12-12 04:17:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!