Take the 2-minute tour ×

I have redefined this question from the original a bit to make it more fundamental to the question at hand. The relevant parts of my filesystem are as follows.


Right now my view.pt template has

<script type="text/javascript" src="/static/myjava.js"></script>

Then in my __init__.py, I have


And finally, the myjava.js file itself is very simple:

document.write("hello from the javascript file")

I am trying to follow this document: http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/assets.html

but right now none of the text is showing up. I feel like the problem lies in the paths i am giving it.

Some ideas I have had: in the config.add_static_view, the name='static' is confusing. I want users to be able to visit the url www.domain.com/firstpage, where firstpage is the result of a template that uses a javascript file resource (a file in the static folder). I am worried that these static assets are only for urls that start with www.domain.com/static/... Is this a valid concern? How can I tell the config.add_static_view function to serve the static resources for any views rendered from the view.pt template?

Edit: here is what worked:

in the template, use src="${request.static_url('tutorial:static/myjava.js')}"

then in the init.py use config.add_static_view(name='static',path='tutorial:static/')

share|improve this question
Dumb question: is tutorial/tutorial/static == env/tutorial/tutorial/static? –  TankorSmash Jan 19 '13 at 23:37
I think so? tutorial is my project folder (so the document root) –  BigBoy1337 Jan 20 '13 at 0:04
add comment

2 Answers

up vote 1 down vote accepted

Your javascript link, in the template, should be something like src="${request.static_url('tutorial:static/myjava.js')}"

This allows your application to be more easily relocated.

This also uses the appropriate asset specification, using the name of the package, "tutorial", a colon, then a path relative to the location of the "tutorial" package, which in your case the package is at env/tutorial/tutorial.

Edited: I forgot about the Configurator object.

Here, you want to use a similar asset specification such as config.add_static_view('static', 'tutorial:static/').

You can make different static views for different directories as well, like: config.add_static_view('images', 'tutorial:images/')

When you do things like this, you can move the root of your application to another location, allowing you to have http://mysite.com/stable/ and http://mysite.com/devel/ having accesses to / be rewritten to /stable/.

The static views can be called from any template with code like ${request.static_url('tutorial:images/icons/favicon.ico')}

share|improve this answer
wow very cool. I guess naming the packages instead of the full path is the way to go. I have updated my question with the now working code. –  BigBoy1337 Jan 25 '13 at 19:37
add comment

Was reading the docs here and it looks like when you call add_static_view it changes the path of the file? To quote the docs:

this means that you wish to serve the files that live in /var/www/static as sub-URLs of the /static URL prefix. Therefore, the file /var/www/static/foo.css will be returned when the user visits your application’s URL /static/foo.css.

In your case, since you're calling env/tutorial/tutorial/static "static", you might want to try src="static/Three.js"> instead

share|improve this answer
I think it would be better to use absolute paths: src="/static/Three.js, or, better, to use static_url(): docs.pylonsproject.org/projects/pyramid/en/1.0-branch/api/… –  Sergey Jan 21 '13 at 5:24
add comment

Your Answer


