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.
env
tutorial
tutorial
templates
view.pt
static
myjava.js
views.py
__init__.py
Right now my view.pt template has
<script type="text/javascript" src="/static/myjava.js"></script>
Then in my __init__.py, I have
config.add_static_view(name='static',path='env/tutorial/tutorial/static')
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 http://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 http://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/’)
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/andhttp://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')}