如何使用 render_to_response 在 Django 中的 div 中呈现 html 页面? [复制]

Posted

技术标签:

【中文标题】如何使用 render_to_response 在 Django 中的 div 中呈现 html 页面? [复制]【英文标题】:How to use render_to_response to render a html page within div in Django? [duplicate] 【发布时间】: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,它又包含额外的数据。我说对了吗? 【参考方案1】:

这不是一个真正的 Django 问题。如果您要从服务器返回要插入页面的数据,您只需使用普通的 jQuery 方法即可:

.success(function(data) 
    $('#info').html(data)
)

【讨论】:

问题是我不能使用成功函数返回的json数据对象。请参考我之前的问题,我已经试过***.com/questions/34788880/… 我想要实现的是 -> 从 index.html 将 id 发送到视图,但渲染 jsComp.html 获取该给定 id 的数据并将其呈现在 index.html 中的 div 中....我认为,这与 Django 相关 好吧,这个问题与那个问题完全一样,不是吗。你怎么又问了?\ 您是否首先阅读了这些问题?我尝试达到要求,但不幸的是,有两种方法没有奏效。请在投票前至少阅读它们

以上是关于如何使用 render_to_response 在 Django 中的 div 中呈现 html 页面? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

django render_to_response 如何发送特殊字符

Django - 在 render_to_response 中使用我的上下文

Django 与 Mako Jinja2 模板比较的集成使用:render_to_response。问题?

render_to_response 给出 TemplateDoesNotExist

django 在 render_to_response() 后注销我

Render_to_response 多个参数 [重复]