gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:55:09 26 4
gpt4 key购买 nike

我需要在 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

我搜索了高低,找不到提供大量静态文件的标准方法。无论我在哪里托管它们(在带有 collectstatic 或 Amazon S3 的 Heroku 上),在我的 urls.py 中明确定义和重定向 14 个文件似乎都不正确。

最佳答案

这是我目前的解决方案。反馈表示赞赏。

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()

关于django - 使用 Django 在 Heroku 上提供根级静态文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11589169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com