admin管理员组文章数量:1298118
I want to load an iframe
into a Django template. The template is getting loaded correctly, but in place of the iframe
, the template itself is getting embedded inside the parent template. The code relevant is given below:
<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 is a file located in the same directory as this HTML template, but it doesn't load. In its place, the parent template itself appears. I looked around Stack Overflow, and from some of the posts I gather that I need to set the src
attribute of the iframe
to a Django view, which will load the iframe
separately. Is this correct? If so, how do I configure the URL (i.e. set the path to the view)?
I want to load an iframe
into a Django template. The template is getting loaded correctly, but in place of the iframe
, the template itself is getting embedded inside the parent template. The code relevant is given below:
<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 is a file located in the same directory as this HTML template, but it doesn't load. In its place, the parent template itself appears. I looked around Stack Overflow, and from some of the posts I gather that I need to set the src
attribute of the iframe
to a Django view, which will load the iframe
separately. Is this correct? If so, how do I configure the URL (i.e. set the path to the view)?
- 1 The src of an iframe is exactly the same as any other URL. If you're expecting Django to serve it, you need to do exactly the same as you would with any other Django page. – Daniel Roseman Commented Feb 1, 2015 at 22:27
2 Answers
Reset to default 23Yes, you have to create the view to load the template. The simplest way to do this is to use generic TemplateView. Add this url to urlpatterns
in your urls.py
:
from django.views.generic import TemplateView
url(r'^testgame/', TemplateView.as_view(template_name="testgame.html"),
name='testgame'),
And <iframe>
tag will look like:
<iframe id="encoder_iframe" height=75% width="50%" src="{% url 'testgame' %}">
</iframe>
If you want to use outside url, you can do like below:
<iframe id="encoder_iframe" height=75% width="50%" src="http://www.example.com/testgame.html"></iframe>
add src with http://
or https://
, otherwise for example it will turn to something like http://127.0.0.1:8000/www.example.com/testgame.html
本文标签: javascriptSet URL to load iframe in a Django templateStack Overflow
版权声明:本文标题:javascript - Set URL to load iframe in a Django template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738487480a2089506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论