正确的静态文件设置

时间:2016-03-23 06:43:51

标签: python django

您好我对设置静态文件非常困惑。无论我尝试什么,每件事都很好(显示图像,javascript,css)。所以我很困惑哪一个是正确的。

目前,这就是我的项目的样子

project
--project
---------static
---------media
--env
--static
--------media
--------static

这是我的代码

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media")

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static")
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

当我执行python manage.py collectstatic时,我没有收到任何错误,但外部静态文件夹中的静态文件夹不包含任何内容。但是静态文件夹中的媒体文件夹包含项目文件夹中媒体文件夹中的文件。

另外 我有这个为aws,

AWS_FILE_EXPIRE = 200
AWS_PRELOAD_METADATA = True
AWS_QUERYSTRING_AUTH = True

DEFAULT_FILE_STORAGE = 'project.utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'project.utils.StaticRootS3BotoStorage'
AWS_STORAGE_BUCKET_NAME = 'realproject'
S3DIRECT_REGION = 'ap-northeast-2'
S3_URL = '//%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = '//%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
STATIC_URL = S3_URL + 'static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

import datetime

date_two_months_later = datetime.date.today() + datetime.timedelta(2 * 365 / 12) 
expires = date_two_months_later.strftime("%A, %d %B %Y 20:00:00 GMT")

AWS_HEADERS = { 
    'Expires': expires,
    'Cache-Control': 'max-age=86400',
} 

有人可以告诉我,我做得对吗?

顺便说一下,我读了https://docs.djangoproject.com/en/1.9/howto/static-files/ 并且跟着它,我不确定我是否正确地遵循它(显示在上面)这就是我要问的原因。

1 个答案:

答案 0 :(得分:0)

python manage.py collectstatic命令查找所有静态目录,并将这些文件合并到STATIC_ROOT设置定义的目录中。

在您的情况下,STATIC_ROOT设置为os.path.join(os.path.dirname(BASE_DIR), "static", "static"),即

your_project/static/static

所以这是收集静态文件的地方。如果您希望它们位于外部静态目录中,则可以将STATIC_ROOT更改为os.path.join(os.path.dirname(BASE_DIR), "static")

在优秀的Django docs here中对此进行了很好的讨论。

在这些设置中有很多内容,因此以下是每个静态设置的快速摘要:

# this is the URL that django will look for static resources at
# - i.e. http://your_domain/static
# so this one is a URL used when by your web server and in template
# shortcuts.
STATIC_URL = '/static/' 

# this is where Django will look for static files to collect. 
# I.e. the search locations that collectstatic uses.  
# 'my_project/static' in this instance. You need to add the places
# you write your static files to this directory. For example, if you
# have several places where you are writing css files, add their
# container directories to this setting.
# it is a list of places to look for static files.
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),) 

# this is where collectstatic will collect the static files to. 
# When you hook this all into your webserver, you would tell your 
# webserver that the /static/ url maps to this directory so that 
# your app can find the static content. It's a directory in your
# project usually.
# it's a single directory where the static files are collected together.
STATIC_ROOT