使用Django在Heroku上提供根级静态文件? [英] Serving root-level static files on Heroku with Django?

查看:179
本文介绍了使用Django在Heroku上提供根级静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Heroku上提供多个根级静态文件(例如, domain.com/favicon.ico )。以下是当前文件列表:

I need to serve several root-level static files on Heroku (eg, domain.com/favicon.ico). Here's the list of files as it currently stands:

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

我搜索过高低,找不到标准的方式来提供一大堆静态文件。无论我在哪里托管他们(在收藏或Amazon S3的Heroku上),明确定义和重定向我的urls.py中的14个文件似乎不正确。

I've searched high and low and can't find a standard way to serve a large set of static files. Wherever I host them (on Heroku with collectstatic or Amazon S3), explicitly defining and redirecting 14 files in my urls.py seems incorrect.

推荐答案

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

This is my current solution. Feedback appreciated.

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在Heroku上提供根级静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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