Reputation: 438
I have the templates for my django app designed by a web design app (axure). So all the static files reference in these template files are relative, such as
<img src="/resources/image/icon.gif"/>
I followed the instruction here https://docs.djangoproject.com/en/dev/howto/static-files/ to modify the settings for the app to include static files and have STATIC_URL fixed. However, by that way, I still have to put this token {{ STATIC_URL }}
in front of every static files reference.
Is there a generic way to do it that makes django to accept these relative paths to static files?
Thank you!
Upvotes: 1
Views: 2499
Reputation: 7631
If you are using a backend like nginx you could set your /staticimage to point to /path/to/static/resources/image/ and then use /staticimage.
I'm sure any web server you are using can be given such a path mapping.
Upvotes: 0
Reputation: 239440
That's not a "relative" path. It's what's called "relative absolute". When you prepend with /
. It tells the browser to reference the following path directly from the domain. This has nothing to do with Django. It has to do with the browser, and they all work that way.
There's nothing special about {{ STATIC_URL }}
; it's just an easy and safe way to prefix your references to static files with the right path. You could actually hard-code the full URL in the your HTML:
<img src="/path/to/static/resources/image/icon.gif"/>
But if your STATIC_URL ever changed, you'd be out of luck. That's why you use {{ STATIC_URL }}
, so that no matter whether it's set to "/static/" or "http://cdn.someprovider.com/myproject/something-else/static/" your references always work.
Long and short, no, there's no "generic way". You have to tell the browser exactly where it can find the file, and that requires using {{ STATIC_URL }}
.
Upvotes: 4