Joe*_*e J 9 css media django static django-staticfiles
我有一个关于新的Django 1.3静态文件框架的一般性问题.
我非常喜欢Django 1.3中引入的新Django静态文件功能.通常,我设置STATIC_URL ="/ static /"并在我的模板中输入{{STATIC_URL}}模板标签.开发服务器自动提供静态文件的方式非常棒,我的所有内容都按预期提供.
The {{ STATIC_URL }} would be substituted in the template and might serve up files like this...
example.com/static/css/master.css
example.com/static/images/logo.png
example.com/static/js/site.js
Run Code Online (Sandbox Code Playgroud)
但是,我正在使用旧网站,其中静态媒体安装在网址根目录.例如,静态URL的路径可能如下所示:
example.com/css/master.css
example.com/images/logo.png
example.com/js/site.js
Run Code Online (Sandbox Code Playgroud)
它不使用"静态"url命名空间.
我想知道是否有办法让新的静态文件功能不使用静态命名空间并提供上面的URL,但仍然保留了新的静态文件框架(开发服务器提供的collectstatic,静态文件等)的好处.我尝试设置STATIC_URL =""和STATIC_URL ="/",但似乎都没有达到预期的效果.
有没有办法配置静态文件来提供没有命名空间的静态文件?谢谢你的考虑.
您可以手动添加static项目目录中不存在的额外位置:
网址.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
)
if settings.DEBUG:
urlpatterns += static('/css/', document_root='app_root/path/to/css/')
urlpatterns += static('/images/', document_root='app_root/path/to/images/')
urlpatterns += static('/js/', document_root='app_root/path/to/js/')
Run Code Online (Sandbox Code Playgroud)
这将为 DEBUG 开发服务器映射媒体。当您运行生产模式服务器时,您显然将从 Web 服务器处理这些静态位置,而不是将请求发送到 django。