更新MEDIA_URL没有反映在的ImageField网址 [英] Updated MEDIA_URL not reflected in ImageField url
问题描述
我试图从服务在Amazon S3上给Amazon CloudFront的文件移动,所以我更新了我的 settings.py
。 previously,我有这样的:
I'm trying to move from serving files on Amazon S3 to Amazon CloudFront, so I updated my settings.py
. Previously, I had this:
S3_URL = 'http://{}.s3.amazonaws.com'.format(AWS_STORAGE_BUCKET_NAME)
STATIC_URL = S3_URL + STATIC_DIRECTORY
MEDIA_URL = S3_URL + MEDIA_DIRECTORY
我更新了这个如下:
I updated this as follows:
#S3_URL = 'http://{}.s3.amazonaws.com'.format(AWS_STORAGE_BUCKET_NAME)
S3_URL = 'http://d2ynhpzeiwwiom.cloudfront.net'
在控制台中的设置被更新:
In the console, the settings are updated:
>>> 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'
怎么办?哪里是旧的信息仍然存储以及如何清理一下?
What gives? Where is the old information still stored and how do I flush it out?
推荐答案
哎呦,原来的 FileField.url
属性不使用 MEDIA_URL
。据<一href="https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.fields.files.FieldFile.url"相对=nofollow> Django的文档:
Whoops, turns out the FileField.url
property doesn't use MEDIA_URL
. According to the Django docs:
FieldFile.url
一个只读属性通过调用底层存储类的网址()方法来访问该文件的相对URL。
A read-only property to access the file’s relative URL by calling the url() method of the underlying Storage class.
在我的情况下,底层存储类,是 S3BotoStorage
按 Django的储量提供了
。正如由现在这个德precated叉,这是previously不支持的,但是现在可以通过添加以下做 settings.py
:
In my case, the underlying storage class, was S3BotoStorage
provided by django-storages
. As noted by this now-deprecated fork, this was previously not supported, but can now be done by adding the following to settings.py
:
AWS_S3_CUSTOM_DOMAIN = 'd2ynhpzeiwwiom.cloudfront.net'
现在它就像一个魅力:
>>> design.image.url
u'https://d2ynhpzeiwwiom.cloudfront.net/media/images/designs/etc/etc'
这篇关于更新MEDIA_URL没有反映在的ImageField网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!