Iframe'd page in Django template can't find it's Javascript or CSS files, 404 error

691 views
Skip to first unread message

Tom Tanner

unread,
Mar 4, 2017, 8:30:37 AM3/4/17
to Django users
In my `views.py`, I have one path render an html file:

    def interactive(request):
    if request.session.get('interactiveID') == None:
    t= get_template('blank-interactive.html')
    else:
    interactive_template= 'interactive-' + str(request.session['id']) + '/index.html'
    t= get_template(interactive_template)
    
    return HttpResponse(t.render())

So `form.html` has an `<iframe>` that requests `/interactive`, and `request.session['id']` is set to `1`. When the iframe requests `/interactive`, it loads `interactive-1/index.html`. The iframe loads the HTML from that page, but cannot get the JS file at `interactive-1/scripts/main.js`. 

When I check the terminal, I see this 404 error: `GET /interactive/scripts/main.js`. If it would just load `/interactive-1/scripts/main.js`, there would be no problem. But Django loads `/interactive-1/index.html`, but not `/interactive-1/scripts/main.js`. Same goes for the CSS and image assets, which are in CSS and image folders.

What do I need to change in Django for it to correctly load the JS, CSS, images and other assets?

Daniel Roseman

unread,
Mar 4, 2017, 8:55:22 AM3/4/17
to Django users
There's not really enough information here to answer your question. How are you referring to the assets in the template? Why do you have a different path for those assets? Note though that template paths and asset paths have nothing whatsoever to do with each other.
-- 
DR.

Tom Tanner

unread,
Mar 4, 2017, 10:57:49 AM3/4/17
to Django users
>How are you referring to the assets in the template?

In `interactive/index.html`, in the `<head>` tag, I have `<script src="scripts/main.js"></script>`

Daniel Roseman

unread,
Mar 4, 2017, 2:53:11 PM3/4/17
to Django users
On Saturday, 4 March 2017 15:57:49 UTC, Tom Tanner wrote:
>How are you referring to the assets in the template?

In `interactive/index.html`, in the `<head>` tag, I have `<script src="scripts/main.js"></script>`

Well, if you wanted to load the scripts from /interactive-1/scripts/main.js, then that's what you should use as the src, surely?
-- 
DR.

chris rose

unread,
Mar 5, 2017, 3:08:36 AM3/5/17
to Django users
there is a lack of information here, though you last post suggests maybe you missed a couple of template tags

at the start of you index.html add:

{% load staticfiles %}

and then when adding you scripts use:

<script src="{% static 'scripts/main.js' %}"></script>

Melvyn Sopacua

unread,
Mar 5, 2017, 10:26:07 AM3/5/17
to django...@googlegroups.com

On Friday 03 March 2017 20:37:29 Tom Tanner wrote:

> When the iframe requests

> `/interactive`, it loads `interactive-1/index.html`.

 

But the iframe still loads /interactive. So it's base url is /interactive and a request for scripts/main.js is /interactive/scripts/main.js.

 

Unless you either:

1/ Do a redirect, which is the simplest

2/ Request your resources incorporating the session id

 

I still would go for 1/, simply because that fixes the disconnect between template path / view path and requested url.

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages