处理Heroku上Django的静态文件和模板的正确方法 [英] Proper way to handle static files and templates for Django on Heroku

查看:137
本文介绍了处理Heroku上Django的静态文件和模板的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的django应用程序移到Heroku,我想知道处理静态文件的正确方法是什么。我只是把它们通过git推到Heroku吗?还是应该将它们存储在SW3上?此外,STATIC_ROOT等应该是什么?



谢谢!

解决方案

p>你应该将它们外部存储在像S3这样的服务上 - 而Heroku可以提供静态文件,这不是为了



这是一个很好的开始使用S3的底稿:



https: //devcenter.heroku.com/articles/s3



使用django-storages http://django-storages.readthedocs.org/en/latest/index.html 将静态文件收集到S3存储区并提供给他们



这些是S3需要的必要设置:

  DEFAULT_FILE_STORAGE ='storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE ='storages.backends.s3boto.S3BotoStorage'

AWS_ACCESS_KE Y_ID ='access-id'
AWS_SECRET_ACCESS_KEY ='secret-key'
AWS_STORAGE_BUCKET_NAME ='bucket-name'
AWS_PRELOAD_METADATA = True#将manage.py collectstatic命令修改为仅上传更改的文件而不是所有文件

MEDIA_ROOT和STATIC_ROOT分别由DEFAULT_FILE_STORAGE和STATICFILES_STORAGE替代,因此不需要。但是,您将要将MEDIA_URL和STATIC_URL设置为

  STATIC_URL ='https://bucket-name.s3 .amazonaws.com / static /'
ADMIN_MEDIA_PREFIX ='https://bucket-name.s3.amazonaws.com/static/admin/'

如果要将静态和媒体文件存储在不同的子文件夹中,这是一个很好的解决方案: https://stackoverflow.com/a/10825691/674794



您需要将MEDIA_URL和STATIC_URL设置为相应的新文件夹,例如

  MEDIA_URL ='https://bucket-name.s3.amazonaws.com/media/'
STATIC_URL ='https://bucket-name.s3.amazonaws.com/static/'

还要手动执行manage.py collectstatic,并按照 https://devcenter.heroku.com/articles/django-assets#disabling_collectstatic ,因为Heroku的收藏者每次推送即使文件未被修改也会将每个静态文件重新上传到S3,添加一个巨大的转移和请求加载到S3,并减慢你的推动。



然后只需继续使用{{STATIC_URL}}在你的模板像平常一样,你应该设置! / p>

 < link href ='{{STATIC_URL}} css / styles.css'type ='text / css'rel ='样式表'> 

如果你想开始简单,选择不立即采取该路线,你可以做通过遵循Cesar提到的 Heroku - 处理静态文件的帖子,快速入侵您的网址配置Django应用程式,但应用程式的成效会明显下降。


I'm moving over my django app to Heroku, and I was wondering what the proper way to handle static files is. Do I just push them via git to Heroku? Or should I be storing them on SW3 or something? Also, what should the STATIC_ROOT and such be?

Thanks!

解决方案

You should store them externally on a service like S3 - while Heroku can serve static files, it's not designed to.

Here's a good primer on getting started with S3:

https://devcenter.heroku.com/articles/s3

Use django-storages http://django-storages.readthedocs.org/en/latest/index.html to collect static files to your S3 bucket and serve them accordingly.

These are the necessary settings you'll need to have for S3:

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

AWS_ACCESS_KEY_ID = 'access-id'
AWS_SECRET_ACCESS_KEY = 'secret-key'
AWS_STORAGE_BUCKET_NAME = 'bucket-name'
AWS_PRELOAD_METADATA = True # necessary to fix manage.py collectstatic command to only upload changed files instead of all files

MEDIA_ROOT and STATIC_ROOT are superceded by DEFAULT_FILE_STORAGE and STATICFILES_STORAGE respectively and hence not needed. You will, however, want to set MEDIA_URL and STATIC_URL to something like

STATIC_URL = 'https://bucket-name.s3.amazonaws.com/static/'
ADMIN_MEDIA_PREFIX = 'https://bucket-name.s3.amazonaws.com/static/admin/'

If you want to store your static and media files in different subfolders, this is a great solution: https://stackoverflow.com/a/10825691/674794

You'll want to set MEDIA_URL and STATIC_URL to the respective new folders, e.g.

MEDIA_URL = 'https://bucket-name.s3.amazonaws.com/media/'
STATIC_URL = 'https://bucket-name.s3.amazonaws.com/static/'

You'll also want to manually execute manage.py collectstatic and disable Heroku's automatic collectstatic as per https://devcenter.heroku.com/articles/django-assets#disabling_collectstatic, as Heroku's collectstatic will reupload every static file to S3 every time you push even if the files haven't been modified, adding a hefty transfer and request load to S3 and slowing down your pushes.

Then just continue using {{ STATIC_URL }} in your templates like usual and you should be set!

<link href='{{ STATIC_URL }}css/styles.css' type='text/css' rel='stylesheet'>

If you want to start off simple and choose not to immediately take that route though, you can do the quick hack in your urls configuration by following Cesar's mentioned post at Heroku - Handling static files in Django app, though there'll be a significant decrease in app performance.

这篇关于处理Heroku上Django的静态文件和模板的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆