首页 > 解决方案 > 将 request.GET 的响应呈现为 iframe

问题描述

我有一个 Django 视图,我需要使用一些参数对 Web 应用程序的 URL 运行 HTTP POST 请求,然后在 iframe 中显示响应。我已经实现了以下代码:

def auth_view(request):

  
    SOME_URL = "SOME URL"
    data = {"some key": "some value",}

    response = requests.post(SOME_URL , data=data)
  
    context = {'content': response.content.decode("utf-8")}

    return TemplateResponse(request, template="portal/return_view.html", context=context)


对于 return_view.html,它看起来像这样:

{{ content | safe }}

这并不重要,但万一是 iframe 运行的 html 如下所示:

<body onload="document.form1.submit()">
    <h1>This is the iframe view</h1>

    <!--  If want to test against development  -->
    <form action="some_url_to_start" method="post" target="tool_content" name="form1">
    
        <input type="hidden" name="some_param" id="some_param" value="{{ some_value }}">
        <button type="submit">
            Launch iframeTool
        </button>
    </form>

    <iframe src="about:blank" width="700" height="600" name="tool_content" id="tool_content" title="Tool Content" ></iframe>
</body>

问题是返回页面正确呈现,看起来像这样:

在此处输入图像描述

问题:呈现的 iframe 页面内部的链接是相对的,但是当单击时,将采用父应用程序的基本 url。有没有办法让渲染的内容采用自己的源基础 url?

第二个问题:底层 iframe 应用程序实际上有一个登录机制,一旦我在这个 iframe 中实现它就无法维持。问题一定出在我这边,因为这个底层应用程序在其他站点实现的 iframe 中运行良好。如何确保登录过程持续进行?

谢谢。

标签: pythondjangoiframe

解决方案


推荐阅读