Search code examples
pythondjangostatic-filesdjango-staticfiles

Django: Static files referring to one another.


I have a directory with static files myapp/static/ and within that there are static/css,static/javascript, and static/images How can these files refere to one another? For instants, the css has to use the image files for the background of certain pages. Do you have to hard code the url? Can you do it relatively?


Solution

  • The static file finders will collocate the contents of all of your static folders under a single root upon collection, which means you can reference static resources relatively.

    For instance, if you had a logo in project/commons/static/images/logo.png, you can reuse it in a stylesheet in some other application, say project/myapp/static/css/myapp.css, by relatively referencing the image, i.e. ../images/logo.png.