[Fixed]-Django favicon.ico in development?

5👍

From the docs:

from django.conf.urls.static import static

urlpatterns = patterns("",
    # Your stuff goes here
) + static('/', document_root='static/')

There doesn’t appear to be a way to serve a single static file, but at least this helper function is a wrapper which only works when DEBUG = True.

👤knite

17👍

The easiest way would be to just put it in your static directory with your other static media, then specify its location in your html:

<link rel="shortcut icon" type="image/png" href="{% static 'images/favicon.ico' %}"/>

My old answer was:

You can set up an entry in your urls.py and just check if debug is true. This would keep it from being served in production. I think you can just do similar to static media.

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^favicon.ico$', 'django.views.static.serve', {'document_root': '/path/to/favicon'}),
    )

You also could just serve the favicon from your view.:

from django.http import HttpResponse

def my_image(request):
    image_data = open("/home/moneyman/public_html/media/img/favicon.ico", "rb").read()
    return HttpResponse(image_data, content_type="image/png")

6👍

This worked for me:

from django.conf.urls.static import static

...

if settings.DEBUG:
    urlpatterns += static(r'/favicon.ico', document_root='static/favicon.ico')

0👍

I use this:

from django import conf
from django.conf.urls import static

...

if conf.settings.DEBUG:
    urlpatterns += static.static(
        r"/favicon.ico", document_root=conf.settings.STATIC_ROOT / "favicon.ico"
    )

-1👍

Well, you can create your own loader.py file, which loads settings you want to override.
Loading this file should look like this:

try:
    execfile(os.path.join(SETTINGS_DIR, 'loader.py'))
except:
    pass

and be added at the end of settings.py.
This settings should not be commited into production server, only should appear on development machines. If you are using git, add loader.py into .gitignore.

👤Maciek

Leave a comment