[Fixed]-Serving root-level static files on Heroku with Django?

4👍

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

2👍

Despite this is being an old question, I still have the problem. I like the way the URLs are generated from a list in the current solution, but I was not convinced that was the best.

After a bit of research though, I found realfavicongenerator.net being a very useful resources which generates all the required favicons files for you and I was sent this post by a colleague which seems to support the proposed solution, with a more recent Django version.

Based on all the above, my solution is using native sitemap framework, django-robots for robots.txt and a ROOT_ASSETS dictionary, with files prefix as key, and values as a list of files, for example:

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",
    ]
}

Then I generate my urls with:

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)),
    [...]
)

I’m also including the HTML generated by realfavicongenerator.net in my master template’s head.

1👍

I didn’t find any url config files, so I think heroku not provide this service.

And according to official docs you need save your data on external services

EDIT:

So according to heroku docs only 301 redirects to S3 can help you. Or even better for all media (ico, favicons, png and another images) files in templates set absolute path to S3 services and for robots and crossdomain.xml in urls.py set 301 redirects to S3. For sitemap.xml better use native solutions

It is not wrong to use right redirects codes in development.

👤b1_

Leave a comment