在开发和生产中提供静态文件的相同且简单的方式-可能吗? [英] Identical and simple way of serving static files in development and production - is it possible?

查看:76
本文介绍了在开发和生产中提供静态文件的相同且简单的方式-可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以仅在PROJECT_DIR/static目录中具有静态文件,而在应用程序目录中没有静态文件的重复,并且不需要执行collectstatic命令?在本地计算机上,生产环境中使用的是Django dev服务器-其他一些Web服务器.根据到目前为止的读物,我认为这是不可能的,但也许并非如此.

Is it possible to have static files only in PROJECT_DIR/static directory without duplicates of static files in app dirs and without needing to do collectstatic command? On local computer Django dev server is used, in production - some other web server. From what I have read so far I decided that it's impossible, but maybe it's not true.

推荐答案

当然可以.静​​态文件应用程序很有用.如果您不喜欢重复"(这很重要-每个应用程序可以包含文件,所有文件都合并到一个区域中),请不要使用staticfiles应用程序.

Of course it's possible.. the static files app is there to be useful. If you dont like "duplicates" (that's the whole point - you can have files per app, all merged into one area), don't use the staticfiles app.

只需在任何位置使用任何文件夹作为资产文件夹.在生产中,使用某些网址(例如 MY_URL )提供该网址.在开发中,连接您的URLConf以在 MY_URL

Just use any folder, anywhere, as your assets folder. In production, serve it at some url, say MY_URL. In development, wire up your URLConf to serve files at your asset folder at MY_URL

https://docs.djangoproject.com/zh-CN/1.5/howto/static-files/#serving-files-up-uped-by-a-user

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static('MY_URL', document_root='path-to-my-files')

在staticfiles带来好处之前,这是一种古老的方法.

This is the old school method of doing this, before staticfiles brought its goodness.

您确定仅使用staticfiles应用程序无法解决此问题吗?在部署脚本中添加 python manage.py collectstatic --noinput 并不需要太多工作.

Are you sure you can't solve this problem by just using the staticfiles app? It's not much work to add in a python manage.py collectstatic --noinput in your deployment script.

这篇关于在开发和生产中提供静态文件的相同且简单的方式-可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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