django在static的子文件夹中未找到静态文件 [英] django not found static files in the subfolder of static
问题描述
django 1.6.5
django 1.6.5
我的静态文件在static的子文件夹下.
My static files is under subfolder of static.
djangoprj
djangoprj
settings.py
urls.py
wsgi.py
__init__.py
static
myapp
mystaticfiles
...
apps
myapp
...
templates
*.html
...
在我的模板文件中,我将静态文件链接为完整路径.
In my templates file, I link static file as full path.
<img src="/static/myapp/images/my.png" />
但是找不到那些文件.我收到404错误.
But those file are not found. I got 404 error.
将settings.py更改为
When change settings.py as
STATIC='/static/myapp/'
我可以得到那些静态文件.但是我不想那样做.为什么在 STATIC ='/static/'
时不起作用?有什么简单的方法可以解决此问题,而不必费力地执行命令,例如"collectstatic"?我已将静态文件放在 static
文件夹中,并且在模板文件中使用了完整路径.为什么不起作用?预先感谢.
I can get those static files. But I am not want to do that. Why dose it not wok, when STATIC='/static/'
? Is there some easy way to solve this problem instead of doing command manaully, such as 'collectstatic'? I have put my static file in static
folder, and I have used full path in my templates file. Why does it not work? Thanks in advance.
推荐答案
现在您将静态文件夹切换为'/static/myapp/'
Now that you switch the static file folder to '/static/myapp/'
您应该在代码中使用< img src ="/static/images/my.png"/>
.
通常,您应该使用 {%static%}
模板标签执行此操作,如下所示(假设 STATIC ='/static/'
).
Generally, you should use {% static %}
template tag to do this, like below (assume STATIC='/static/'
).
{% load staticfiles %}
<img src="{% static 'images/myapp/my.png' %}" />
以了解更多信息,请参见教程 https://docs.djangoproject.com/en/1.6/intro/tutorial06/
我建议您阅读 https://docs.djangoproject中的所有教程(第1部分-第6部分).com/en/1.6/
以便您对基础有足够的了解.
So that you can know deep enough for the basis.
这篇关于django在static的子文件夹中未找到静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!