Skip to content
Advertisement

Set URL to load iframe in a Django template

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:

JavaScript

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)?

Advertisement

Answer

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:

JavaScript

And <iframe> tag will look like:

JavaScript
Advertisement