作者:淡淡笑嘻嘻 | 来源:互联网 | 2023-08-07 18:21
Iamtryingtodeploymydjangoapponheroku.Allthestaticfilesareservedbywhitenoiseandare
I am trying to deploy my django app on heroku. All the static files are served by whitenoise
and are migrated successfully. But how do I get my media files that are uploaded using ImageField
to display during production.
我正在尝试在heroku上部署我的django应用程序。所有静态文件都由whitenoise提供并成功迁移。但是如何在生产期间显示使用ImageField上载的媒体文件。
my settings.py
我的settings.py
STATIC_URL = '/static/'
STATIC_ROOT = 'C:/Users/Sak/mpro/feat/static/'
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
wsgi.py
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mpro.settings")
application = get_wsgi_application()
try:
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
from dj_static import Cling
application = Cling(get_wsgi_application())
except:
pass
urls.py
urls.py
urlpatterns = [
url(r'^', include('feat.urls', namespace="feat")),
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
All my media files are uploaded to base_dir/media/media
and when whitenoise
collects static files it only collects files at STATIC_ROOT
, so how do I get my media files deployed. When I run heroku run ls
I can see the folder media.
我的所有媒体文件都上传到base_dir / media / media,当whitenoise收集静态文件时,它只收集STATIC_ROOT的文件,那么如何部署我的媒体文件。当我运行heroku运行时,我可以看到文件夹媒体。
1 个解决方案
5
So, first of all it's important to realise that if you have a bunch of files that are part of your project (including things like images and such) and are deployed along with it, they are more likely to be static files than media files in Django parlance.
所以,首先重要的是要意识到如果你有一堆文件属于你的项目(包括像图像之类的东西)并随之一起部署,它们更可能是静态文件而不是媒体文件。 Django的说法。
Media files in Django are for runtime uploaded files. They will generally be stored with a corresponding db entry such as models.ImageField(upload_to='/photos')
but that isn't necessarily so.
Django中的媒体文件用于运行时上传的文件。它们通常与相应的数据库条目一起存储,例如models.ImageField(upload_to ='/ photos'),但不一定如此。
Your media setup is wrong for Heroku, as you shouldn't be writing to a directory on the server anyway (this is what you are telling Django to do with MEDIA_ROOT = os.path.join(BASE_DIR, "media")
.
Heroku的媒体设置是错误的,因为你不应该写入服务器上的目录(这就是你告诉Django使用MEDIA_ROOT = os.path.join(BASE_DIR,“media”)。
Most commonly you would instead use an S3 bucket for media on Heroku. This is a fairly comprehensive explanation of that. You can probably skip the bit about CORS and go for open permissions on the bucket (depending on your use case). The important bits are:
最常见的是你会在Heroku上使用S3存储桶作为媒体。这是一个相当全面的解释。您可以跳过有关CORS的内容并获取存储桶的开放权限(具体取决于您的使用案例)。重要的是:
# settings.py
# Tell django what URL to server your media from
MEDIA_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
# Tell Django to use the boto storage backend to save media files.
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
If you want to use S3 for static AND media storage, the link explains that too.
如果要将S3用于静态和媒体存储,该链接也会解释这一点。
You might want to look at this repo for a pretty comprehensive run-down of the AWS settings and what they do, plus a nicely wrapped package to do some of it for you.
您可能希望查看此repo以获得相当全面的AWS设置及其功能,以及一个包装精美的程序包,以便为您完成一些操作。
More generally I found Django Herokuify exceptionally useful for all the boilerplate on Heroku.
更一般地说,我发现Django Herokuify非常适用于Heroku上的所有样板。