由于原因问题导致无法将TinyMCE js文件存储在我的S3存储桶上,我正试图让Heroku为它们提供服务。
这是我到目前为止所尝试的但没有运气。浏览器网址看起来不错(http://www.mysite.com/media/js/tiny_mce/tiny_mce.js),但heroku不提供服务并返回404.
这是我的代码:
Settings.py
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
MEDIA_URL = '/media/'
TINYMCE_JS_URL = MEDIA_URL + 'js/tiny_mce/tiny_mce.js'
TINYMCE_JS_ROOT = MEDIA_ROOT + 'js/tiny_mce'
urls.py
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_URL}))
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
答案 0 :(得分:2)
我可以使用以下代码直接从heroku提供静态资源:
settings.py:
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
MEDIA_URL = '/media/'
TINYMCE_JS_URL = MEDIA_URL + 'js/tiny_mce/tiny_mce.js'
TINYMCE_JS_ROOT = MEDIA_ROOT + 'js/tiny_mce'
urls.py:
urlpatterns = patterns('',
...
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True, }),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True, }),
)
base.html文件:
<script type="text/javascript" src="{{ MEDIA_URL }}js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
mode: "textareas",
theme: "advanced",
forced_root_block: false,
force_p_newlines : false,
force_br_newlines : true,
});
</script>
答案 1 :(得分:0)
好的,使用github讨论https://github.com/aljosa/django-tinymce/pull/15
中的评论我主要更改了urls.py:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': '/app/.heroku/python/lib/python2.7/site-packages/tinymce/static/'}))
我有一种感觉,这可以更好地解决,但我没有想法,这是有效的
答案 2 :(得分:0)
对于Django> = 2.0.0,要直接从heroku服务MEDIA_URL
,可以使用
from django.urls import include, path, re_path
from django.views.static import serve
urlpatterns = [
...
re_path(r'^media/(?P<path>.*)$', serve,
kwargs=dict(document_root=settings.MEDIA_ROOT)),
]
请记住,heroku每次部署都会删除MEDIA_ROOT文件夹。
更多信息 https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted