Identical and simple way of serving static files in development and production - is it possible?

丶灬走出姿态 提交于 2019-12-13 17:40:47

问题


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.


回答1:


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.

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/en/1.5/howto/static-files/#serving-files-uploaded-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')

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

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.



来源:https://stackoverflow.com/questions/16829928/identical-and-simple-way-of-serving-static-files-in-development-and-production

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!