django staticfiles at url root [英] django staticfiles at url root

查看:199
本文介绍了django staticfiles at url root的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于新的Django 1.3静态文件框架的一般问题。

I have a general question about the new Django 1.3 static file framework.

我真的很喜欢Django 1.3中引入的新Django静态文件功能。通常,我设置STATIC_URL =/ static /,然后在我的模板中输入{{STATIC_URL}}模板标记。开发服务器自动提供静态文件并且所有内容都按预期提供,这是很好的。

I really like the new Django staticfile functionality introduced in Django 1.3. Normally, I set STATIC_URL="/static/" and enter the {{ STATIC_URL }} template tag into my templates. It's great how the development server automatically serves up the static files and all of my content is served up as expected.

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

但是,我使用的是一个遗留网站,静态媒体安装在url根目录下。例如,静态网址的路径可能如下所示:

However, I'm working with a legacy site where the static media is mounted at the url root. For example, the path to the static urls might look something like this:

example.com/css/master.css
example.com/images/logo.png
example.com/js/site.js 

它不使用staticurl命名空间。

It doesn't use the "static" url namespace.

我想知道是否有一种方法可以使新的staticfile功能不使用静态命名空间并提供上面的url,但仍然保留新的staticfile的好处框架(collectstatic,由开发服务器提供的静态文件等)。我尝试设置STATIC_URL =和STATIC_URL =/,但似乎没有想要的效果。

I was wondering if there is a way to get the new staticfile functionality to not use the static namespace and serve the urls above, but still retain the benefits of the new staticfile framework (collectstatic, static files served by development server, etc). I tried setting STATIC_URL="" and STATIC_URL="/", but neither seemed to have the desired effect.

有没有办法配置静态文件来提供没有命名空间的静态文件?感谢您的考虑。

Is there a way to configure static files to serve static files without a namespace? Thanks for your consideration.

推荐答案

您可以手动添加 static 您的项目中的目录:

You can manually add extra locations that do not exist within the static directories within your project:

urls.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/')

这将映射DEBUG dev服务器的媒体。当你运行你的生产模式服务器,你显然将处理这些静态位置从Web服务器,而不是发送请求到django。

This will map the media for the DEBUG dev server. And when you are running your production mode server, you will obviously be handling these static locations from the web server instead of sending the request through to django.

这篇关于django staticfiles at url root的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆