使用 Django 在 Heroku 上提供根级静态文件?

问题描述 投票:0回答:4

我需要在 Heroku 上提供几个根级静态文件(例如,

domain.com/favicon.ico
)。以下是当前的文件列表:

favicon.ico
crossdomain.xml
sitemap.xml
robots.txt
humans.txt
apple-touch-icon-57x57-precomposed.png
apple-touch-icon-57x57.png
apple-touch-icon-72x72-precomposed.png
apple-touch-icon-72x72.png
apple-touch-icon-114x114-precomposed.png
apple-touch-icon-114x114.png
apple-touch-icon-precomposed.png
apple-touch-icon.png

我到处搜索,但找不到提供大量静态文件的标准方法。无论我在哪里托管它们(在 Heroku 上使用 Collectstatic 或 Amazon S3),在我的 urls.py 中显式定义和重定向 14 个文件似乎都是不正确的。

django heroku favicon
4个回答
4
投票

这是我目前的解决方案。感谢反馈。

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from settings import STATIC_URL, ASSETS, DEBUG, AWS_STORAGE

#ASSETS is a tuple: ('favicon.ico, 'robots.txt', ...)    
urls = [('^%s$' % f, 'redirect_to', {'url': STATIC_URL + f}) for f in ASSETS]
urlpatterns += patterns('django.views.generic.simple', *urls)

#Serve static files from runserver if in dev mode with S3 off.
if DEBUG and not AWS_STORAGE:
    urlpatterns += staticfiles_urlpatterns()

2
投票

尽管这是一个老问题,但我仍然有问题。我喜欢当前解决方案中从列表生成 URL 的方式,但我不相信这是最好的。

经过一番研究,我发现 realfavicongenerator.net 是一个非常有用的资源,它可以为您生成所有必需的图标文件,我是由一位同事发送的 这篇文章,他似乎支持建议的解决方案,并带有更新的 Django 版本。

基于上述所有内容,我的解决方案是使用本机站点地图框架,django-robots用于robots.txt和ROOT_ASSETS字典,以文件前缀作为键,值作为文件列表,例如:

ROOT_ASSETS = {
    "images/favicons/": [
        "apple-touch-icon-114x114.png",
        "apple-touch-icon-120x120.png",
        "apple-touch-icon-144x144.png",
        "apple-touch-icon-152x152.png",
        "apple-touch-icon-180x180.png",
        "apple-touch-icon-57x57.png",
        "apple-touch-icon-60x60.png",
        "apple-touch-icon-72x72.png",
        "apple-touch-icon-76x76.png",
        "apple-touch-icon-precomposed.png",
        "favicon.ico",
    ]
}

然后我生成我的网址:

from django.conf.urls import patterns, url, include
from django.contrib.staticfiles.storage import staticfiles_storage

root_assets_urls = []
for prefix, files in ROOT_ASSETS.iteritems():
    for f in files:
        asset_url = staticfiles_storage.url("{prefix}{file}".format(prefix=prefix, file=f))
        root_assets_urls.append(
            url(r'^{0}$'.format(f), RedirectView.as_view(url=asset_url))
        )
root_assets = patterns('', *root_assets_urls)

urlpatterns = patterns(
    '',
    url(r'^', include(root_assets)),
    [...]
)

我还将由 realfavicongenerator.net 生成的 HTML 包含在我的主模板的头部中。


1
投票

我没有找到任何url配置文件,所以我认为heroku不提供此服务。

根据官方文档,您需要将数据保存在外部服务上

编辑:

因此根据heroku 文档,只有 301 重定向到 S3 可以帮助您。或者更好的是,对于模板中的所有媒体(ico、favicons、png 和其他图像)文件,设置 S3 服务的绝对路径,对于

urls.py
中的机器人和 crossdomain.xml 设置 301 重定向到 S3。为了更好地使用 sitemap.xml 本机解决方案

在开发中使用正确的重定向代码并没有错。


0
投票

我在 Heroku 上使用 Django 5.x。

首先,我尝试从根目录提供文件,而不使用 302 重定向到由whitenoise 控制的静态目录。不过太麻烦了。

我的第二次尝试受到这些巧妙解决方案的启发:AB

示例:

/favicon-32x32.png
重定向至
/static/favicon-32x32.9e902d163167.png

这是

urls.py
的片段。

from django.contrib.staticfiles.storage import staticfiles_storage
from django.views.generic import RedirectView

def favicon_urlpatterns():
    names = [
        'android-chrome-192x192.png',
        'android-chrome-384x384.png',
        'apple-touch-icon.png',
        'browserconfig.xml',
        'favicon-16x16.png',
        'favicon-32x32.png',
        'favicon.ico',
        'mstile-150x150.png',
        'safari-pinned-tab.svg',
        'site.webmanifest'
    ]
    urlpatterns = []
    for name in names:
        item = path(name, RedirectView.as_view(url=staticfiles_storage.url(name)))
        urlpatterns.append(item)
    return urlpatterns

# At the bottom of urls.py
urlpatterns += favicon_urlpatterns()

这是我的

settings.py
的片段。图标文件放置在
root_static
目录中。

STATIC_ROOT = BASE_DIR / "staticfiles"
STATIC_URL = "static/"

ROOT_STATIC = BASE_DIR / 'root_static'
STATICFILES_DIRS = [
    ROOT_STATIC,
]
© www.soinside.com 2019 - 2024. All rights reserved.