设置 URL 以在 Django 模板中加载 iframe
Set URL to load iframe in a Django template
我想将iframe
加载到 Django 模板中。模板正在正确加载,但代替iframe
,模板本身嵌入到父模板中。相关代码如下:
<body>
<p>
<strong>Player: {{player.username}}</strong>
<div id="playerid">{{player.id}}</div><br>
<iframe id="encoder_iframe" height=75% width="50%" src="testgame.html"></iframe>
<br>
<strong>Last score:</strong>
<span id="scores"></span><br><br>
<strong>Game state:</strong>
<div id="gamestate"></span>
</p>
<br>
</body>
testgame.html 是一个与此 HTML 模板位于同一目录中的文件,但它不会加载。在其位置上,将显示父模板本身。我环顾了一下 Stack Overflow,从我收集的一些帖子中,我需要将iframe
的 src
属性设置为 Django 视图,这将单独加载iframe
。这是对的吗?如果是这样,如何配置 URL(即设置视图的路径)?
是的,您必须创建视图才能加载模板。 执行此操作的最简单方法是使用通用模板视图。 将此 URL 添加到urls.py
中的urlpatterns
:
from django.views.generic import TemplateView
url(r'^testgame/', TemplateView.as_view(template_name="testgame.html"),
name='testgame'),
<iframe>
标签将如下所示:
<iframe id="encoder_iframe" height=75% width="50%" src="{% url 'testgame' %}">
</iframe>
如果你想使用外部 url,你可以这样做:
<iframe id="encoder_iframe" height=75% width="50%" src="http://www.example.com/testgame.html"></iframe>
添加带有 http://
或 https://
的 SRC ,否则例如它将变成类似 http://127.0.0.1:8000/www.example.com/testgame.html
相关文章:
- 使用php脚本验证访问者的年龄,并在不刷新的情况下根据结果加载iframe
- 使用Javascript(或jQuery)加载iframe内容
- Internet Explorer重新加载IFrame
- Fine Uploader-未收到来自已加载iframe的有效消息
- 使用本地生成的代码通过 JavaScript 加载 IFrame
- 加载iframe时激发的事件
- 动态渲染 .mht 文件,无需加载/iframe/嵌入
- 如何在选择列表项时重新加载iframe
- 如何使用JavaScript在iframe中重新加载iframe
- 在featherlight.js中延迟加载iFrame
- 实时在更改时重新加载iframe脚本
- 如何在不闪烁的情况下加载Iframe
- 如何按时间间隔重新加载Iframe内容,使其不会闪烁
- 设置 URL 以在 Django 模板中加载 iframe
- 在离开页面之前加载 iframe
- 加载 Iframe src,并从水平下拉菜单中进行选择
- 同时加载 iframe 并重置文档位置
- 单击按钮时重新加载 iframe 内容(本地 HTML)
- 单击元素时重新加载 iframe src
- 在可见时加载 iframe