Reputation: 138
I have some javascript that I would like to run on a particular page of a Django site I am building. In the template I currently have:
<script type='text/javascript' src='{% static "/home_page/github_repos.js" %}'></script>
The HTML that is generated by this is:
http://localhost:8000/home_page/github_repos.js
The problem is that I have the javascript in /static/home_page/
.
If I hardcode in the path as:
<script type='text/javascript' src='static/home_page/github_repos.js'></script>
Everything works fine.
However, I am getting odd behavior that I do not understand:
If, in the template I set the path like this:
<script type='text/javascript' src='{% static "static/home_page/github_repos.js" %}'></script>
The HTML generate has a src attribute of /static/static/home_page/github_repos.js
The part that is really throwing me off is that if I do not put /static/
at the front of the src when using the template tags (like the first example I give), /static/
is not added, however, if I do add /static/
at the front, it gets added. I need /static/
added to the front of the src attribute, but only once.
Some relevant information:
My home_page app resolves to '/' as the url, so localhost:8000/
gets you to the home page.
I have included'django.contrib.staticfiles'
in my installed apps
Finally, STATIC_URL = '/static/'
Is this problem occurring because my home_page app resolves to the root address of localhost:8000/
?
Any help is appreciated.
Upvotes: 0
Views: 494
Reputation: 27102
This issue is because the path in the static
tag should be relative to the static directory. From the docs:
Uses the configured
STATICFILES_STORAGE
storage to create the full URL for the given relative path
You're using an absolute path. Instead, you should use:
{% static "home_page/github_repos.js" %}
Note the omission of the leading forward slash.
Upvotes: 1