Reputation: 1564
I'm using cherrypy with jinja2 templates from a 'views' directory like this:
env = Environment(loader = FileSystemLoader('views'))
When I render the index page:
index = env.get_template('index.html')
it shows up fine, but referenced css, javascripts and images (inside the index.html) are not resolved:
<head>
<link rel="stylesheet" type="text/css" media="screen" href="css/web.css">
<link rel="shortcut icon" type="image/png" href="img/favicon.png">
<script src="js/jquery-1.7.1.min.js" type="text/javascript" charset="utf-8"></script>
</head>
the file system is organized like: views/css , views/js , views/img
What is a robust way to obtain working file resolution?
Upvotes: 2
Views: 3713
Reputation: 22659
Static paths in Jinja's templates have nothing to do with Jinja. It's all about the configuration of the web server. If you're using flask, use SharedDataMiddleware
dispatcher for static files, i.e.
from werkzeug import SharedDataMiddleware
app.wsgi_app = SharedDataMiddleware(app.wsgi_app,
{ '/static': '/path/to/static/files' } )
The structure of the static directory:
.../static/
img/
css/
js/
etc/
Don't forget to add slash at the beginning of the paths:
<link rel="shortcut icon" type="image/png" href="/static/img/favicon.png">
It is strongly discouraged to use SharedDataMiddleware
on production servers. Nginx is the right thing.
Upvotes: 1