简体   繁体   English

如何将文件从 Django 管理员直接上传到 Google Cloud Storage,以及如何在模板中呈现它们?

[英]How do I upload files from Django admin directly to Google Cloud Storage, and how do I render them in the templates?

I am trying to store images in Google Cloud Storage for a production website.我正在尝试将图像存储在 Google Cloud Storage 中以用于生产网站。 I have google cloud storage currently and I am wondering how to have the images uploaded in the Django admin store in GCS and how to render these in the templates.我目前有谷歌云存储,我想知道如何在 GCS 的 Django 管理存储中上传图像以及如何在模板中呈现这些图像。

I have tried assigning the MEDIA_URL to the GCS bucket but they are not storing.我尝试将 MEDIA_URL 分配给 GCS 存储桶,但它们没有存储。 Basically I just need some guidance from the start on how to achieve this.基本上,我只需要从一开始就需要一些关于如何实现这一目标的指导。

You do not need to load them in Django, you can just link them.您不需要在 Django 中加载它们,您只需链接它们即可。

  • Make the desired bucket publicly available .使所需的存储桶公开可用

  • Then, just reference the image in the template.然后,只需引用模板中的图像。

Finally, you can always create a Model in the DB to store the links to the images in a relational way.最后,您始终可以在数据库中创建一个模型,以关系方式存储到图像的链接。

You need to install this package: https://django-storages.readthedocs.io/en/latest/index.html after you need to read this guidehttps://docs.djangoproject.com/en/3.0/topics/http/file-uploads/你需要安装这个包: https : //django-storages.readthedocs.io/en/latest/index.html后你需要阅读本指南https://docs.djangoproject.com/en/3.0/topics/http /文件上传/

and to provide a proper handler to you FileField (or ImageField )并为您提供适当的处理程序FileField (或ImageField

If you did everything well, then the admin site should work properly.如果您一切顺利,那么管理站点应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM