【问题标题】:heroku - django images wont work in heroku but works fine locallyheroku - django 图像不能在 heroku 中工作,但在本地工作正常
【发布时间】:2012-08-05 09:44:42
【问题描述】:

我是 Django 和 Heroku 的新手。我的安装在本地运行良好,但是当它推送到 Heroku 时,我无法在我的站点中看到 css、js 或图像。

这是我的网址格式:

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'blog.views.home', name='home'),
    # url(r'^blog/', include('blog.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    url(r'^comments/', include('django.contrib.comments.urls')),
    url(r'^blog/', include('zinnia.urls')),
    url(r'^', include('cms.urls')),
)

if settings.DEBUG:
    urlpatterns = patterns('',
        (r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
    ) + urlpatterns

这是我的 settings.py

STATIC_ROOT = os.path.join(PROJECT_PATH, "static")

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

请让我知道我哪里出错了。提前致谢。

干杯 S

【问题讨论】:

    标签: django heroku django-cms


    【解决方案1】:

    要在 Heroku 上提供静态文件,您需要使用额外的应用程序,例如 whitenoise。我遵循了这些步骤,并能够在 Heroku 上提供图像。

    此链接是主要指南: https://github.com/codingforentrepreneurs/Guides/blob/master/all/Heroku_Django_Deployment_Guide.md

    这些将是参考资料(我建议您也仔细阅读参考资料):

    1)http://whitenoise.evans.io/en/stable/django.html

    2)https://docs.djangoproject.com/en/3.0/howto/static-files/

    3)https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#std:templatetag-static

    我遇到了您遇到的同样问题。遵循指南和参考中的步骤对我来说效果很好。

    【讨论】:

    • 为什么他们做了一个不完整的服务,所以我们需要使用外部使用,这根本没有任何意义。
    【解决方案2】:

    对于 Heroku,您的 DEBUG 设置是否设置为 True?如果没有,appmedia.urls 将不会被包含在内。

    在相关说明中,django-appmedia 并不是在 Django 中处理静态资产的最佳方式——从 Django 1.3 开始,有一个名为 staticfiles 的 contrib 应用程序(参见:https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/),Heroku 预计您将使用该应用程序。

    此外,django-appmedia 似乎希望资产位于每个应用的 /media/ 目录中,而 staticfiles 期望资产位于每个应用的 /static/ 文件夹中。

    您是否查看过https://devcenter.heroku.com/articles/django-assets 上有关 Django 和静态资产的 Heroku 文档?

    【讨论】:

    • 嗨杰森。我已经在使用 staticfiles 应用程序,并且我在本地收集静态文件。同样,这在本地运行良好,但在 Heroku 上却不行。
    • 我还检查了图像 url 及其从静态文件夹中提供的服务。例如:“/static/cms/images/pony.jpg”
    • 另一个有趣的发现。即使在本地,图像和 css 也仅使用 django 内置服务器和带有 gunicorn 或 foreman start 的 NIT 提供......
    • 您的 urls.py 文件(在您的原始评论中发布的文件)中没有任何内容告诉 gunicorn 如何找到静态资产。例如,在我现在在 Heroku 上的一个暂存应用程序中,我的项目级 urls.py 中有以下几行(显然 Stack Overflow cmets 不允许回车): from django.contrib.staticfiles.urls import staticfiles_urlpatterns 和然后 urlpatterns += staticfiles_urlpatterns()。这样临时应用程序就可以通过 gunicorn 提供静态文件。 (生产应用将资产推送到 S3。)
    • 嗨杰森。我添加了这个,它现在可以找到 - urlpatterns += patterns('', (r'^static(?P.*)$', 'django.views.static.serve', {'document_root': settings .STATIC_ROOT}), ) 非常感谢...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 2019-10-12
    • 2013-10-31
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多