Python 从数据库中存储的HTML呈现iframe
我有一个使用Django呈现的HTML模板。我正在传递所有必要的上下文变量-Python 从数据库中存储的HTML呈现iframe,python,html,django,iframe,django-templates,Python,Html,Django,Iframe,Django Templates,我有一个使用Django呈现的HTML模板。我正在传递所有必要的上下文变量-my_heading是标题,my_html是标记安全的html。我想在iframe中显示我的html <html> <head> <title>Iframe Example</title> </head> <body> <p>{% my_heading %}</p>
my_heading
是标题,my_html
是标记安全的html。我想在iframe中显示我的html
<html>
<head>
<title>Iframe Example</title>
</head>
<body>
<p>{% my_heading %}</p>
<iframe name="iframe1" width="600" height="400" src="http://www.yahoo.com" frameborder="yes" scrolling="yes"></iframe>
</body>
</html>
Iframe示例
{%my_heading%}
你知道怎么做吗?我找到的所有示例都显示了指向URL的iframe。我对HTML和JS非常糟糕|
谢谢如果我理解正确,您只想在Django模板中显示您用Python动态准备的HTML内容,对吗?如果是这种情况,则不需要Iframe。只有当您希望将不同URI(网页)的部分集成为页面上的框架(网页上的部分)时,才需要iFrame。以下是一个示例:
<html>
<head>
<title>Iframe Example</title>
</head>
<body>
<p>{% my_heading %}</p>
<div id="content">{% my_html %}</div>
</body>
</html>
Iframe示例
{%my_heading%}
{%my_html%}
除上述代码外,根据下面的注释进行添加。听起来您想用Iframe显示整个页面,而第二个页面来自您的框上的另一个视图/url。然后使用:
<html>
<head>
<title>Iframe Example</title>
</head>
<body>
<p>{% my_heading %}</p>
<iframe src="URL_TO_MY_2ND_VIEW_IN_DJANGO" width="100%" height="300"></iframe>
</body>
</html>
Iframe示例
{%my_heading%}
如果我理解正确,您只想在Django模板中显示用Python动态准备的HTML内容,对吗?如果是这种情况,则不需要Iframe。只有当您希望将不同URI(网页)的部分集成为页面上的框架(网页上的部分)时,才需要iFrame。以下是一个示例:
<html>
<head>
<title>Iframe Example</title>
</head>
<body>
<p>{% my_heading %}</p>
<div id="content">{% my_html %}</div>
</body>
</html>
Iframe示例
{%my_heading%}
{%my_html%}
除上述代码外,根据下面的注释进行添加。听起来您想用Iframe显示整个页面,而第二个页面来自您的框上的另一个视图/url。然后使用:
<html>
<head>
<title>Iframe Example</title>
</head>
<body>
<p>{% my_heading %}</p>
<iframe src="URL_TO_MY_2ND_VIEW_IN_DJANGO" width="100%" height="300"></iframe>
</body>
</html>
Iframe示例
{%my_heading%}
迈克尔·科洛克没有回答你的问题,但他给出的答案是一个更好的解决方案。你宁愿那样做
但要回答您的问题:您只能将url传递给IFrame,所以如果您真的想使用IFrame,您需要在django中设置第二个url+视图,以返回my_html作为响应。即,将发生2个http请求。1个用于包含IFrame的页面,1个用于请求IFrame的内容 Michael Klocker没有回答你的问题,但他给出的答案是更好的解决方案。你宁愿那样做
但要回答您的问题:您只能将url传递给IFrame,所以如果您真的想使用IFrame,您需要在django中设置第二个url+视图,以返回my_html作为响应。即,将发生2个http请求。1个用于包含IFrame的页面,1个用于请求IFrame的内容 我用这个黑客来做的。div是隐藏的,包含HTML。IFrame没有指向某个位置,因为我不希望出现任何CORS问题。然后我将HTML注入其中
<html>
<head>
<title>Iframe Example</title>
</head>
<body>
<p>{% my_heading %}</p>
<iframe name="iframe2" id="iframe2" width="0" height="0" src="" style="border:0px; overflow-x:hidden;"></iframe>
<div id="page" style="display:none" >{{ my_html }}</div>
<script type="text/javascript">
function getWindow(iframe) {
return (iframe.contentWindow) ? iframe.contentWindow : (iframe.contentDocument.document) ? iframe.contentDocument.document : iframe.contentDocument;
}
getWindow(document.getElementById('iframe2')).document.open();
getWindow(document.getElementById('iframe2')).document.write(document.getElementById('page').innerHTML);
getWindow(document.getElementById('iframe2')).document.close();
document.getElementById('iframe2').style.height = (getWindow(document.getElementById('iframe2')).document.body.scrollHeight + 20) +"px";
document.getElementById('iframe2').style.width = (document.getElementById('iframe2').parentNode.offsetWidth) +"px";
</script>
</body>
</html>
Iframe示例
{%my_heading%}
{{my_html}}
函数getWindow(iframe){
return(iframe.contentWindow)?iframe.contentWindow:(iframe.contentDocument.document)?iframe.contentDocument.document:iframe.contentDocument;
}
getWindow(document.getElementById('iframe2')).document.open();
getWindow(document.getElementById('iframe2')).document.write(document.getElementById('page').innerHTML);
getWindow(document.getElementById('iframe2')).document.close();
document.getElementById('iframe2').style.height=(getWindow(document.getElementById('iframe2')).document.body.scrollHeight+20)+“px”;
document.getElementById('iframe2').style.width=(document.getElementById('iframe2').parentNode.offsetWidth)+“px”;
我用这个黑客做的。div是隐藏的,包含HTML。IFrame没有指向某个位置,因为我不希望出现任何CORS问题。然后我将HTML注入其中
<html>
<head>
<title>Iframe Example</title>
</head>
<body>
<p>{% my_heading %}</p>
<iframe name="iframe2" id="iframe2" width="0" height="0" src="" style="border:0px; overflow-x:hidden;"></iframe>
<div id="page" style="display:none" >{{ my_html }}</div>
<script type="text/javascript">
function getWindow(iframe) {
return (iframe.contentWindow) ? iframe.contentWindow : (iframe.contentDocument.document) ? iframe.contentDocument.document : iframe.contentDocument;
}
getWindow(document.getElementById('iframe2')).document.open();
getWindow(document.getElementById('iframe2')).document.write(document.getElementById('page').innerHTML);
getWindow(document.getElementById('iframe2')).document.close();
document.getElementById('iframe2').style.height = (getWindow(document.getElementById('iframe2')).document.body.scrollHeight + 20) +"px";
document.getElementById('iframe2').style.width = (document.getElementById('iframe2').parentNode.offsetWidth) +"px";
</script>
</body>
</html>
Iframe示例
{%my_heading%}
{{my_html}}
函数getWindow(iframe){
return(iframe.contentWindow)?iframe.contentWindow:(iframe.contentDocument.document)?iframe.contentDocument.document:iframe.contentDocument;
}
getWindow(document.getElementById('iframe2')).document.open();
getWindow(document.getElementById('iframe2')).document.write(document.getElementById('page').innerHTML);
getWindow(document.getElementById('iframe2')).document.close();
document.getElementById('iframe2').style.height=(getWindow(document.getElementById('iframe2')).document.body.scrollHeight+20)+“px”;
document.getElementById('iframe2').style.width=(document.getElementById('iframe2').parentNode.offsetWidth)+“px”;
你好,迈克尔。我一定遗漏了一些信息。我所说的HTML是一个包含所有CSS、JS等内容的完整页面。我想我不能在div
中显示它,是吗?div
适合我需要显示的一个小HTML片段。希望我的回答是正确的。是的,你是正确的。如果要在标记中显示整个页面,则需要使用Iframe标记。我将更改上面的代码。看看我的答案。我总算把它拼凑起来了,太棒了。我很高兴您找到了自己问题的解决方案:-)因此,如果您有一个视图可以加载但集成来自另一个url的数据,如您的iframe示例所示,那么您必须使用iframe,或者是否有其他选项?我一直在用它来处理图像,但现在我想用它来处理纯文本/JSON响应。我一定遗漏了一些信息。我所说的HTML是一个包含所有CS的完整页面