【问题标题】:correct static files setting正确的静态文件设置
【发布时间】:2016-03-23 06:43:51
【问题描述】:

您好,我对设置静态文件感到非常困惑。无论我尝试什么,每件事都可以正常工作(显示图像、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/ 并遵循它,我不确定我是否正确遵循它(如上所示),这就是我问的原因。

【问题讨论】:

    标签: python django


    【解决方案1】:

    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 
    

    【讨论】:

    • 是的,谢谢,这就是我感到困惑的原因,那么我的媒体文件怎么会收集在 my_project/static/media 中的文件夹中
    • 这是由于您的 MEDIA_ROOT 设置,即MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media")my_project/static/media。同样,更改此设置会导致它们在其他地方结束。
    • 我现在很困惑,我阅读了更多关于这方面的教程,我得到了更多的困惑。我试着像你说的那样改变,但静态文件没有被收集到静态文件夹中。我只是要离开它 MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media") STATIC_ROOT = '' STATIC_URL = '/static /' STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
    • 一开始很令人困惑——有很多设置——不过你会到达那里的:)。我将在答案中添加一些细节以尝试提供更多帮助
    • 基本上,STATICFILES_DIRS 的列表是 collectstatic 获取文件 FROM 的位置,STATIC_ROOTcollectstatic 放置文件的位置 TO乙>。 STATIC_URL 是网络服务器通过 URL 查找文件的地方。
    猜你喜欢
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2011-10-11
    • 2013-04-12
    • 1970-01-01
    • 2013-03-20
    相关资源
    最近更新 更多