【问题标题】:Updated MEDIA_URL not reflected in ImageField url更新的 MEDIA_URL 未反映在 ImageField url 中
【发布时间】:2014-07-11 21:25:37
【问题描述】:

我正在尝试从 Amazon S3 上的服务文件转移到 Amazon CloudFront,因此我更新了我的 settings.py。以前,我有这个:

S3_URL = 'http://{}.s3.amazonaws.com'.format(AWS_STORAGE_BUCKET_NAME)
STATIC_URL = S3_URL + STATIC_DIRECTORY
MEDIA_URL = S3_URL + MEDIA_DIRECTORY

我更新如下:

#S3_URL = 'http://{}.s3.amazonaws.com'.format(AWS_STORAGE_BUCKET_NAME)
S3_URL = 'http://d2ynhpzeiwwiom.cloudfront.net'

在控制台中,设置被更新:

>>> from django.conf import settings
>>> settings.MEDIA_URL
'http://d2ynhpzeiwwiom.cloudfront.net/media/'

但不在我模型的ImageField

>>> design.image.url
'https://bucketname.s3.amazonaws.com/media/images/designs/etc/etc'

什么给了?旧信息还存储在哪里,如何清除?

【问题讨论】:

  • 你重启服务器了吗?
  • 我想通了——一秒钟内回答自己

标签: django amazon-s3 django-storage


【解决方案1】:

哎呀,原来FileField.url 属性不使用MEDIA_URL。根据the Django docs

FieldFile.url

一个只读属性,通过调用底层存储类的 url() 方法来访问文件的相对 URL。

在我的例子中,底层存储类是S3BotoStorage,由django-storages 提供。正如this now-deprecated fork 所指出的,这以前不受支持,但现在可以通过将以下内容添加到settings.py 来完成:

AWS_S3_CUSTOM_DOMAIN = 'd2ynhpzeiwwiom.cloudfront.net'

现在它就像一个魅力:

>>> design.image.url
u'https://d2ynhpzeiwwiom.cloudfront.net/media/images/designs/etc/etc'
猜你喜欢
  • 2014-04-27
  • 1970-01-01
  • 2014-07-24
  • 1970-01-01
  • 2017-02-12
  • 1970-01-01
  • 2015-12-12
  • 2020-03-30
  • 2021-05-07
相关资源
最近更新 更多