我一直在尝试使用heroku在生产过程中提供静态文件但是它不起作用。
我正在使用Django 1.8和python 2.7。
我尝试了很多东西:
我尝试过这些解决方案:
Proper way to handle static files and templates for Django on Heroku
还有很多其他人。
我当前的静态设置是
AWS_STORAGE_BUCKET_NAME = 'BUCKET_NAME'
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_PRELOAD_METADATA = True
AWS_S3_SECURE_URLS = True
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
模板示例
<!DOCTYPE html>
{% load static from staticfiles %}
<html>
<head>
<meta charset="UTF-8">
<title>TITLE</title>
</head>
<body>
<img src="{% static "images/404.jpg" %}" alt="Page Not Found (404)." style="position: absolute; left: 50%; top: 50%; margin-left: -285px; margin-top: -190px;">
<img src="{% static "medcstatic/images/404.jpg" %}" alt="Page Not Found (404)." style="position: absolute; left: 50%; top: 50%; margin-left: -285px; margin-top: -190px;">
</body>
</html>
我的网址看起来像这样
urlpatterns = [..
]
if not settings.DEBUG:
urlpatterns += (r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
如果有人能帮助我,我会非常感激。
答案 0 :(得分:2)
所以我终于解决了这个问题,并认为我应该把这件衣服留在这里。 我基本上重做了整个事情,并按照这个链接 https://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/
只需确保从开始就在AWS中设置权限。
pull-right
}
正确的角色设置:
{
"Statement": [
{
"Sid":"PublicReadForGetBucketObjects",
"Effect":"Allow",
"Principal": {
"AWS": "*"
},
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::BUCKET-NAME/*"
]
},
{
"Action": "s3:*",
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::BUCKET-NAME",
"arn:aws:s3:::BUCKET-NAME/*"
],
"Principal": {
"AWS": [
"USER-ARN"
]
}
}
]