处理与Django中的应用无关的静态文件 [英] Handling static files that dont pertain to an app in Django
问题描述
在文档 https://docs.djangoproject.com/en/ dev / howto / static-files /
我读到静态文件应该放在各自的应用程序中,并用
{%load staticfiles%}
< img src ={%staticarticles / css / base.css%}alt = 我的形象/>
不过,在文档中,它提到一些静态文件与一个特定的应用无关。这是 STATICFILES_DIRS
发挥作用的地方。如果我正确地读取 STATICFILES_DIRS
是Django用来查找其他静态文件的元组。我想知道如何调用从 STATICFILES_DIRS
中调用的静态文件?
ex: / p>
< link rel =stylesheettype =text / csshref ={%static/css/default.css% }>
另外我不知道为我的 STATIC_ROOT
。我把它留空吗? (''
)
我的项目树
mysite
\articles
\static
\articles
\css
base.css
\\ \\static
\images
\css
default.css
\js
\templates
base.html
\ settings.py
这是目前在我的settings.py关于静态文件
#在每个应用程序中查找静态文件
STATICFILES_FINDERS =(
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_STORAGE =(
'django.contrib.staticfiles.storage.StaticFilesStorage'
)
#collectstatic将收集静态文件进行部署(OUTPUT)的绝对路径
STATIC_ROOT =''
#此设置定义如果启用FileSystemFinder查找器,静态文件应用将遍历的其他位置。
STATICFILES_DIRS =(
#用于不绑定到特定应用程序的静态资产
os.path.join(BASE_DIR,'static'),
)
#引用位于STATIC_ROOT
中的静态文件时要使用的URL STATIC_URL ='/ static /'
几乎所有关于django static的内容都与 django.contrib.staticfiles
应用程序有关。虽然您需要自定义编辑许多设置以使 staticfiles
工作,但是做的很简单。它提供了一个 collectstatic
命令,它从不同的应用程序收集静态文件,并将它们放入一个目录。
答案对于您的第一个问题很简单:将这些常见的静态文件放在django项目目录的 / static
目录下。在你的情况下,它是 mysite / static
。
原因:首先,这是正式的方式。您可以在官方文档中找到以下代码:管理静态文件(CSS,图像) 。第二,这是合理的。因为我们把静态文件只用在一个单一的应用程序在 project / appnane / static /...
项目的静态目录应该遵循相同的名称模式。
STATICFILES_DIRS =(
os.path.join(BASE_DIR,static),#就这样!!
' var / www / static /',
)
正如我在评论中所说,不应将 STATIC_ROOT
设置为 project_absolutr_path / static
。因为那个目录是用户把css应用程序的静态文件。您不希望collectst命令污染该目录,特别是在使用版本控制系统(如git / svn)时。
STATIC_ROOT
真的取决于你托管这些静态文件的方式(Apache,Nginx,S3,CDN,Paas like heroku)
In the documentation https://docs.djangoproject.com/en/dev/howto/static-files/
I read that static files should be put with their respective apps and called upon with
{% load staticfiles %}
<img src="{% static "articles/css/base.css" %}" alt="My image"/>
However later on in the docs it mentions that some static files don't pertain to a particular app. This is where STATICFILES_DIRS
comes into play. If I read correctly STATICFILES_DIRS
is a tuple for Django to use to look for other static files. I was wondering how would I call the static files that was called from the STATICFILES_DIRS
?
ex: something like
<link rel="stylesheet" type="text/css" href="{% static "/css/default.css" %}">
Also I am not sure what to put for my STATIC_ROOT
. Do I leave it empty? (''
)
My proj tree
mysite
\articles
\static
\articles
\css
base.css
\static
\images
\css
default.css
\js
\templates
base.html
\settings.py
This is currently in my settings.py regarding static files
# looks for static files in each app
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_STORAGE = (
'django.contrib.staticfiles.storage.StaticFilesStorage'
)
# the absolute path to the directory where collectstatic will collect static files for deployment (OUTPUT)
STATIC_ROOT = ''
# This setting defines the additional locations the static files app will traverse if the FileSystemFinder finder is enabled.
STATICFILES_DIRS = (
# used for static assets that aren't tied to a particular app
os.path.join(BASE_DIR, 'static'),
)
# URL to use when referring to static files located in STATIC_ROOT
STATIC_URL = '/static/'
Almost everything about django static is related to the django.contrib.staticfiles
app. Although you need to custom edit many settings to make staticfiles
work, what is does is simple. It provides a collectstatic
command which collects static files from different app and put them into a single directory.
The answer to your first question is simple: Put those common static files under the /static
directory of your django project directory. In your case, it's mysite/static
.
Reason: First, it's the official way. You can find the following code in official doc: Managing static files (CSS, images). Second, it's reasonable. Since we put static files only used in a single app under project/appnane/static/...
The project's static dir should follow the same name pattern.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"), # That's it!!
'/var/www/static/',
)
As I said in the comment, your should not set STATIC_ROOT
to project_absolutr_path/static
. Because that directory is user to put css app static files. You don't want the collectstatics command to pollute that directory especially when you are using a version control system like git/svn.
STATIC_ROOT
really depends on the way you host these static files(Apache, Nginx, S3, CDN, Paas like heroku)
这篇关于处理与Django中的应用无关的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!