django静态文件奇怪的行为
在我的 settings.py 中我有:
import os
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
# ... #
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
# ... #
STATIC_URL = '/static/'
在我的 urls.py 中我有:
urlpatterns = patterns('',
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
在我的模板中:
<img src="{{ STATIC_URL }}images/myimage.png">
哪个不起作用。奇怪的是,如果我将 settings.py:
STATIC_URL = 'static/'
和模板更改为:
<img src="/{{ STATIC_URL }}images/myimage.png">
确实有效!我一直在兜圈子来解决这个问题,并且我查看了很多论坛帖子,但我似乎无法弄清楚我做错了什么。
in my settings.py I have:
import os
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
# ... #
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
# ... #
STATIC_URL = '/static/'
in my urls.py I have:
urlpatterns = patterns('',
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
and in my template:
<img src="{{ STATIC_URL }}images/myimage.png">
Which doesn't work. The weird thing is that if I change my settings.py:
STATIC_URL = 'static/'
and my template to:
<img src="/{{ STATIC_URL }}images/myimage.png">
does work!! I have been chasing round in circle to fix this, and I have looked at lots of forum posts, but I can't seem to figure out what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 staticfiles 应用程序,则不需要这个(假设您使用的是 Django 1.3):
它由应用程序自动处理。尝试删除并看看会发生什么。
if you use staticfiles app you don't need this (assuming you are using Django 1.3):
Its handled by the app automatically. Try to remove and see what happens.