我正在构建一个Django网站,用户可以通过在网站上区分它们的表单上传图像。这些保存在/ grad / media /文件夹中(其中grad是应用程序的名称)。
这是完成此任务的代码:
uni_object = University.objects.get(user=request.user)
logo = uni_object.logo
if request.method == "POST":
form = UpdateUniversityForm(request.POST, request.FILES)
if form.is_valid():
data = form.cleaned_data
request.user.email = data['email']
request.user.save()
uni_object.university_name = data['university_name']
uni_object.website = data['website']
if request.FILES and request.FILES['logo'] != '':
new_logo = data['logo']
image = Image.open(new_logo)
image = image.resize((400,400), Image.ANTIALIAS)
thumb_io = StringIO.StringIO()
image.save(thumb_io,format='JPEG')
new_file_name = str(int(time.time()))
thumb_file = InMemoryUploadedFile(thumb_io, None, '{0}.jpg'.format(new_file_name), 'image/jpeg',
thumb_io.len, None)
uni_object.logo = thumb_file
uni_object.save()
updated = True
logo = uni_object.logo
基本上我需要将它转换为400x400的方格,然后保存它。此代码适用于localhost,文件将正确保存。
然而在Heroku中,DEBUG = False:
好像媒体文件夹基本上完全被忽略了,尽管查看了heroku日志,但在尝试访问它时我得到了一个HTTP 200:
2016-10-22T15:39:13.652167+00:00 heroku[router]: at=info method=GET path="/media/1477150753.jpg"
host=www.gradba.se request_id=7e545ef5-92cf-406c-88c8-26d140541884 fwd="31.50.136.136,141.101.98.8"
dyno=web.1 connect=0ms service=21ms status=200 bytes=6354
有人能告诉我为什么会这样吗?我认为media_root设置正确,因为它适用于开发。
谢谢!