简体   繁体   中英

Django: {{STATIC_URL}} tag not working when url path is extended

I'm using {{STATIC_URL}} all over the place in my templates (for simplification, we'll used the homepage as an example). So, I've got a parent template called "home_base.html", which uses the tag in situations like this:

<script src="{{STATIC_URL}}js/plugins.js">
<img src={{STATIC_URL}}img/blah.jpg>

In my urls.py file, I've got two urls, that point to the same view :

url(r'^home/$', 'homepage.views.HomeView'),
url(r'^home/x/$', 'homepage.views.HomeView', {'x': '3'}),

When I visit mysite.com/home, everything works fine. However, when I visit mysite.com/home/x, none of the files in the static folder of the app can be found. I get errors that look like this, for each static file in the template:

[30/Jan/2013 13:04:20] "GET /homepage/static/js/plugins_functions.js HTTP/1.1" 404 7325
[30/Jan/2013 13:04:20] "GET /homepage/static/js/backb_global.js HTTP/1.1" 404 7310

It seems like it's adding /homepage/ to the beginning...usually it gets /static/js...

I have found that it has something to do with adding stuff to the url path , because if I change my urls to look like this, everything works perfectly:

url(r'^home/$', 'homepage.views.HomeView'),
url(r'^x/$', 'homepage.views.HomeView', {'x': '3'}),

Why does everything get messed up when I add slashes to the url? How can I fix it? I would really like to just add things to home like this: url(r'^home/add/stuff/to/address/$' , and still have all the {{STATIC_URL}} tags working.

Keep in mind that I'm using this parent template as a base for many other templates, so it's important for me that I can add to the path of the url, and keep the {{STATIC_URL}} tags in the home_base.html file.

尝试使用转义字符串变量

{{STATIC_URL|safe}}

Did you check https://docs.djangoproject.com/en/dev/howto/static-files/ where it says:

If {{ STATIC_URL }} isn't working in your template, you're probably not using RequestContext when rendering the template.

and:

make sure 'django.core.context_processors.static' is in your TEMPLATE_CONTEXT_PROCESSORS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM