所以我制作了這個應用程式,您可以在其中制作產品。當我在本地使用此應用程式時,這些產品的影像已保存在檔案中。我在 heroku 和 ofc 上部署了我的應用程式,因為沒有像 localy 這樣的普通檔案無法保存在那里。我怎樣才能保存它并使用它?我也使用 postgesql btw 來存盤其他資料,所以也許我也可以存盤影像。
class Product(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=255, blank=True, null=True)
image = models.ImageField(upload_to='images', blank=True, null=True)
price = models.FloatField(max_length=255, blank=True, null=True)
description = models.TextField(max_length=255, blank=True, null=True)
category = models.ForeignKey(Category, blank=True, on_delete=models.CASCADE)
seller = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
uj5u.com熱心網友回復:
heroku 不能保存我們需要在某處托管我們的檔案的靜態檔案。如果您想使用免費的靜態檔案提供程式,那么您可以查看https://pypi.org/project/django-cloudinary-storage/ .. 還有另一個是 AWS S3 存盤桶,我們也可以使用它,但它會向我們收取 1 美元一年..
云設定:
pip 安裝 django-cloudinary-storage
#在setting.py中匯入
匯入 cloudinary 匯入 cloudinary.uploader 匯入 cloudinary.api
INSTALLED_APPS = [ ....... 'cloudinary_storage', 'cloudinary',
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'
#如果你去https://cloudinary.com/documentation/django_integration你可以獲得鑰匙
CLOUDINARY_STORAGE = { 'CLOUD_NAME': '', 'API_KEY': '', 'API_SECRET': '' }
###########
然后再次部署
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/351084.html
標籤:Python 姜戈 PostgreSQL 英雄
下一篇:型別錯誤:預期的str、bytes或os.PathLike物件,而不是Django中的JpegImageFile(或PngImageFile)
