【问题标题】:How to upload and use images on heroku django如何在heroku django上上传和使用图片
【发布时间】:2021-12-19 23:26:56
【问题描述】:

所以我制作了这个应用程序,您可以在其中制作产品。当我在本地使用这个应用程序时,这些产品的图像保存在一个文件中。我在 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

【问题讨论】:

    标签: python django postgresql heroku


    【解决方案1】:

    heroku 无法保存我们需要在某处托管文件的静态文件。 如果您想使用免费的静态文件提供程序,那么您可以查看https://pypi.org/project/django-cloudinary-storage/ .. 还有一个是 AWS S3 存储桶,我们也可以使用它,但它会向我们收取 1 美元一年的费用..

    云端设置:

    pip install django-cloudinary-storage

    #import in setting.py

    导入云 导入 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':'' }

    ##########

    然后再次部署

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 2012-03-28
      • 2022-11-11
      • 2012-08-06
      • 1970-01-01
      相关资源
      最近更新 更多