Django和staticfiles问题 [英] Django and staticfiles questions

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

问题描述



我正在阅读官方文档,stackoverflow和google,但我仍然有疑问。



组织静态文件的正确/正常方式是什么?



我的意思是文件夹和settings.py



我有这样的东西:

  CURRENT_PATH = os.path.dirname(__ file__)

STATIC_ROOT = os.path.join(CURRENT_PATH,'static')

STATIC_URL ='/ static /'

好的,我要收集所有我的应用程序静态在〜/ static /



我创建了一个静态/每个应用的appname文件夹,我把我所有的应用程序的静态存在。



另外,我需要一个静态文件夹到项目范围的静态(它的常用名称是什么?我使用/静态/收集的东西,他们不能相等)。



到目前为止这么好,我有css像:

  href ={{STATIC_URL}} appname / styles.css

,它的作用就像魅力。



但是我认为当我部署我的应用程序时,运行'collectstatic',所以我把这个'/ static /'文件夹服务在切诺基。



问题是...会工作吗?我尝试评论AppDirectoryFinder和_DIRS,并且在本地不工作(收集静态的东西,我的意思是css on / static /和其他文件夹)。



最好在根目录中为所有项目安装一个静态文件夹?并将管理员css复制到该文件夹​​(手动收集AKA)。



我在github / bitbucket上看到的项目已准备好部署,我需要知道步骤



谢谢!

解决方案

我将使用django静态应用程序



url,将您的静态媒体投放到
STATIC_URL ='/ static /'
这是用于您的模板中的两件事物
{{STATIC_URL}},静态文件url
以及django中的静态文件(只开发

  from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
urlpatterns + = staticfiles_urlpatterns()

您的文件位于服务器上的位置

  STATIC_ROOT ='/ var / www / website / static'

收集,是你的w ebserver应该正在寻找



您的文件查找器定义

  STATICFILES_FINDERS =( 
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',

我使用django的默认值,您当然可以使用更多,但这里是您要查找的关键问题


'django.contrib.staticfiles.finders.AppDirectoriesFinder'


会找到在安装的应用程序内的任何名为static的文件夹


'django.contrib.staticfiles.finders.FileSystemFinder'


将告诉django查看STATICFILES_DIRS(这是您的项目宽静态文件)



其中应定义为元组

  STATICFILES_D IRS =(
join(CURRENT_PATH,'static'),

其中'static'是你想要的,你可以添加许多其他文件夹来监视你想要的。



你放在每个应用程序内的子目录ie:app /静态/ appname不是必需的,但会确保不同应用程序内的同名文件不会覆盖其他应用程序或根静态文件夹中的文件。



所有这些都是取自我自己的经验和 https://docs.djangoproject.com/en/ 1.3 / ref / contrib / staticfiles /


The more I learn Django, the more I discover new things.

I read the official docs, stackoverflow and google but I still have doubts.

What's the correct/normal way to organize our static files?

I mean folders and settings.py

I have something like:

CURRENT_PATH = os.path.dirname(__file__)

STATIC_ROOT = os.path.join(CURRENT_PATH, 'static')

STATIC_URL = '/static/'

Ok, Im going to collect all my apps statics on ~/static/

I created a static/appname folder on every app and I put all my app's static there.

Also, I need a static folder to project-wide statics (what's the common name for it? Since I used /static/ for collected stuff and they cannot be equal).

So far so good, I have css like:

href="{{ STATIC_URL }}appname/styles.css"

and it works like charm.

But I think that when I deploy my app, I have to run 'collectstatic' so I put that '/static/' folder serving on Cherokee.

The question is... will that work? I tried commenting the AppDirectoryFinder and the _DIRS one and that doesn't work on local (Having the static stuff collected, I mean, the css on /static/ and in the other folders too).

Is just better to have one static folder on root for all the project? And copy the admin css to that folder (AKA manually collectstatic).

The projects I see on github/bitbucket are ready to be deployed, I need to know the steps to go from dev to deploy.

Thanks!

解决方案

I'll break this down as I use the django static app

url at which your static media will be served STATIC_URL = '/static/' this is used for 2 things {{STATIC_URL}} in your templates and the static file url and for hosting your static files in django (DEVELOPMENT ONLY)

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

The location at which your files reside on the server

STATIC_ROOT = '/var/www/website/static'

this is used when you run collectstatic and is where your webserver should be looking

your file finder definition

STATICFILES_FINDERS = ( 
                       'django.contrib.staticfiles.finders.FileSystemFinder',
                       'django.contrib.staticfiles.finders.AppDirectoriesFinder',
                       )

I've used the default from django you can of course use more but here is the crux of what you are looking to know

'django.contrib.staticfiles.finders.AppDirectoriesFinder'

will find any folder named "static" that is inside an installed app

'django.contrib.staticfiles.finders.FileSystemFinder'

will tell django to look at STATICFILES_DIRS (this is your project wide static files)

which should be defined as a tuple

STATICFILES_DIRS = ( 
                    join( CURRENT_PATH, 'static' ),
                    )

where 'static' is whatever you want and you can add in as many other folders to monitor as you wish.

the sub directories you place inside each app ie:app/static/appname are not necessary but will ensure that files of the same name inside different apps don't overwrite files from other apps or your root static folders

all of this was taken from my own experience and https://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/

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

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