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>&nbsp;
        <div id="playerid">{{player.id}}</div><br>
        <iframe id="encoder_iframe" height=75% width="50%" src="testgame.html"></iframe>
        <br>
        <strong>Last score:</strong>&nbsp;
        <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>&nbsp;
        <div id="playerid">{{player.id}}</div><br>
        <iframe id="encoder_iframe" height=75% width="50%" src="testgame.html"></iframe>
        <br>
        <strong>Last score:</strong>&nbsp;
        <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)?

Share Improve this question edited Nov 28, 2021 at 17:38 Nick is tired 7,05521 gold badges41 silver badges54 bronze badges asked Feb 1, 2015 at 22:17 user3033194user3033194 1,8217 gold badges44 silver badges64 bronze badges 1
  • 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
Add a comment  | 

2 Answers 2

Reset to default 23

Yes, 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