简体   繁体   English

Django和staticfiles问题

[英]Django and staticfiles questions

The more I learn Django, the more I discover new things. 我学习Django的次数越多,发现新事物的机会就越多。

I read the official docs, stackoverflow and google but I still have doubts. 我阅读了官方文档,stackoverflow和google,但我仍然有疑问。

What's the correct/normal way to organize our static files? 组织静态文件的正确/正常方式是什么?

I mean folders and settings.py 我的意思是文件夹和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/ 好的,我要在〜/ static /上收集我所有的应用程序静态信息

I created a static/appname folder on every app and I put all my app's static there. 我在每个应用程序上都创建了一个static / appname文件夹,并将所有应用程序的静态文件都放在那里。

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). 另外,我需要一个静态文件夹来存放整个项目范围的静态数据(它的通用名称是什么?因为我将/ static /用于收集的内容,所以它们不能相等)。

So far so good, I have css like: 到目前为止,到目前为止,我的CSS如下:

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. 但是我认为,当我部署应用程序时,必须运行“ collectstatic”,因此我将“ / static /”文件夹放在切诺基上。

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). 我尝试对AppDirectoryFinder和_DIRS之一进行注释,但这在本地不起作用(我收集了静态内容,我的意思是在/ static /上的css以及其他文件夹中)。

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). 并将admin css复制到该文件夹​​(又称为手动收集静态文件)。

The projects I see on github/bitbucket are ready to be deployed, I need to know the steps to go from dev to deploy. 我在github / bitbucket上看到的项目已准备好进行部署,我需要知道从开发人员进行部署的步骤。

Thanks! 谢谢!

I'll break this down as I use the django static app 我将在使用Django静态应用程序时对此进行分解

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 ) 静态媒体将投放到的网址STATIC_URL ='/ static /',用于模板中的两件事{{STATIC_URL}}和静态文件网址,以及将静态文件托管在django中( 用于开发

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 当您运行collectstatic时,将使用它,这是您的Web服务器应在其中查找的位置

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的默认值,您当然可以使用更多默认值,但这是您想要了解的关键

'django.contrib.staticfiles.finders.AppDirectoriesFinder' 'django.contrib.staticfiles.finders.AppDirectoriesFinder'

will find any folder named "static" that is inside an installed app 将在已安装的应用程序中找到任何名为“ static”的文件夹

'django.contrib.staticfiles.finders.FileSystemFinder' 'django.contrib.staticfiles.finders.FileSystemFinder'

will tell django to look at STATICFILES_DIRS (this is your project wide static files) 会告诉django查看STATICFILES_DIRS(这是您项目范围内的静态文件)

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 您放置在每个应用程序内的子目录(即:app / static / appname)不是必需的,但可以确保不同应用程序内具有相同名称的文件不会覆盖其他应用程序或根静态文件夹中的文件

all of this was taken from my own experience and https://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/ 所有这些都是根据我自己的经验和https://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/

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). 另外,我需要一个静态文件夹来存放整个项目范围的静态数据(它的通用名称是什么?因为我将/ static /用于收集的内容,所以它们不能相等)。

Are you sure? 你确定吗? I'm pretty sure I'm using the same folder to collect my static files and to hold my project-wide static files. 我很确定我正在使用同一文件夹来收集我的静态文件并保存我的项目范围内的静态文件。 Not sure if that's not recommended pracice, but it works for me. 不知道这是否不建议实践,但是对我有用。

Note that this is just on the deployment side; 注意,这只是在部署方面。 my codebase just has the project static files. 我的代码库只有项目静态文件。

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

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