简体   繁体   English

dlango staticfiles at url root

[英]django staticfiles at url root

I have a general question about the new Django 1.3 static file framework. 我有一个关于新的Django 1.3静态文件框架的一般问题。

I really like the new Django staticfile functionality introduced in Django 1.3. 我非常喜欢Django 1.3中引入的新Django静态文件功能。 Normally, I set STATIC_URL="/static/" and enter the {{ STATIC_URL }} template tag into my templates. 通常,我设置STATIC_URL =“/ static /”并在我的模板中输入{{STATIC_URL}}模板标签。 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

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: 例如,静态URL的路径可能如下所示:

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

It doesn't use the "static" url namespace. 它不使用“静态”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). 我想知道是否有办法让新的静态文件功能不使用静态命名空间并提供上面的URL,但仍然保留了新的静态文件框架(开发服务器提供的collectstatic,静态文件等)的好处。 I tried setting STATIC_URL="" and STATIC_URL="/", but neither seemed to have the desired effect. 我尝试设置STATIC_URL =“”和STATIC_URL =“/”,但似乎都没有达到预期的效果。

Is there a way to configure static files to serve static files without a namespace? 有没有办法配置静态文件来提供没有命名空间的静态文件? Thanks for your consideration. 谢谢你的考虑。

You can manually add extra locations that do not exist within the static directories within your project: 您可以手动添加项目中static目录中不存在的额外位置:

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

This will map the media for the DEBUG dev server. 这将映射DEBUG开发服务器的媒体。 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. 当您运行生产模式服务器时,您显然会从Web服务器处理这些静态位置,而不是将请求发送到django。

why not keep the staticfiles functionality and simply use rewrites at the web server level to serve up the content. 为什么不保留静态文件功能,只需在Web服务器级别使用重写来提供内容。

for instance: 例如:

rewrite /css /static permanent; (for nginx) 

this would keep your project directory a lot cleaner and also make it easier to move your static directories around in the future, for instance to move to your STATIC_URL to a CDN. 这样可以使您的项目目录更加清晰,并且可以在将来更轻松地移动静态目录,例如将STATIC_URL移动到CDN。

This is how you set up your urls.py to serve both index.html and your other static files on / in Django 1.10 (while still being able to serve other Django views): 这就是你如何设置你的urls.py来为Django 1.10上的/上提供index.html和其他静态文件(同时仍然能够提供其他Django视图):

from django.contrib.staticfiles.views import serve
from django.views.generic import RedirectView

urlpatterns = [

    # / routes to index.html
    url(r'^$', serve,
        kwargs={'path': 'index.html'}),

    # static files (*.css, *.js, *.jpg etc.) served on /
    url(r'^(?!/static/.*)(?P<path>.*\..*)$',
        RedirectView.as_view(url='/static/%(path)s')),
]

See this answer where I wrote a more complete explanation of such a configuration – especially if you want to use it for production. 看到这个答案 ,我写了一个更完整的解释,特别是如果你想用它来生产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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