Search code examples
htmlcsstornadostatic-files

Tornado: url in external css file


In tornado's html template, one recommendation about specifying static file path is that we should use static_url rather than hard-coding the path. For example,

 <link rel="stylesheet" type="text/css" href="{{static_url("css/frontpage.css")}}">    

But it doesn't work if I try to do something similar in the external "frontpage.css" file, such as

body { 
background-image: static_url("img/bgimg.jpg");
background-repeat: no-repeat;} 

Of course, I can hard-code the path as "/static/img/bgimg.jpg" to avoid this. However, I think there should be a more proper way to handle this. Any suggestions would be appreciated!


Solution

  • Since your url in the css is defined as a relative url you should be fine, because it will be relative to the css file. The reason that you want to use static_url in the html template is so that you can easily relocate the static assets to a CDN or another web server so that it is not being served from the tornado instance.