Why aren't static assets loading when I access my pyramid app through my computer's IP address?

StackOverflow https://stackoverflow.com/questions/9086472

  •  21-04-2021
  •  | 
  •  

Question

I have a Pyramid app that I've been developing and viewing locally through http://localhost:6543, which is what the paster docs suggest. There are various assets, for example .css files, available in a static directory, which I have made available through config.add_static_view('static','static') When I view it through localhost everything works fine, my .css files are loaded, and all is well. However, when I view the app through my computer's hostname/IP address, the static assets aren't loaded.

A freshly-installed Pyramid paster scaffold displays the same behavior. I've gone through and followed the Pyramid narrative documentation through the step called viewing the application, and everything works as is described. But change the URL in the location bar from http://localhost:6543 to http://my.host.name:6543 and the style sheets don't get loaded.

The assets are available; type http://my.host.name:6543/static/pylons.css on a freshly-created Pyramid paster scaffold and you can read the contents of the css, but it's not loaded when the root page is loaded. Firebug indicates that those resources are requested, but never received.

What's going on here, and how can I make sure that my static assets are loaded when I request them through something other than localhost?

Edit to add some code. This is from the Pyramid starter paster scaffold, which I haven't modified and which exhibits the same behavior my application does; it can be considered a minimal example.

from package's __init__.py:

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings)
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.scan()
    return config.make_wsgi_app()

complete views.py:

from pyramid.view import view_config

@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
    return {'project':'TestProject'}

relevant line from templates/mytemplate.pt:

<link rel="stylesheet" href="/static/pylons.css" type="text/css" media="screen" 
charset="utf-8" />

Where static/pylons.css is in the static directory under the root.

Again, opening http://0.0.0.0:6543 in the browser (FF10) works fine, http://my.host.name:6543 displays the page without style information; but http://my.host.name:6543/static/pylons.css gives me the style sheet text.

Was it helpful?

Solution

Without being able to look at your code it's a bit hard to answer this question. My guess based on a similar experience is that you might not be using the static_url method inside your templates. Below is another way to reference static assets in your templates:

<link rel="stylesheet" href="${request.static_url('app:static/css/app.css')}">

Are you using that? Using anything else will certainly cause the behaviour you are seeing.

There seems to be a problem if the application is installed to a path that is not the document route. You can see this discussed here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top