正确的静态文件设置

2024-04-20 13:41:57 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我对设置静态文件很困惑。无论我做什么,每件事都很好(显示图像、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的时候管理.pycollectstatic,我没有得到任何错误,但是外部静态文件夹中的静态文件夹不包含任何内容。但静态文件夹中的媒体文件夹包含项目文件夹中的媒体文件夹中的文件。你知道吗

还有 我有这个给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/ 我不确定我是否正确地遵循了它(如上图所示),这就是为什么我要问这个问题。你知道吗


Tags: pathproject文件夹awsurlbaseos静态
1条回答
网友
1楼 · 发布于 2024-04-20 13:41:57

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")。你知道吗

在优秀的Djangodocs 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 

相关问题 更多 >