Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django favicon.ico in development?

Tags:

favicon

django

How can I serve favicon.ico in development? I could add a route in my urlconf, but I don't want that route to carry over to the production environment. Is there a way to do this in local_settings.py?

like image 813
knite Avatar asked Jun 23 '12 22:06

knite


People also ask

How set favicon Ico in Django?

Add the favicon.ico pathImport staticfiles_storage and the Django RedirectView from the appropriate Django directories. Then add the favicon path to the list of URL patterns. This path will redirect to the ICO file located in the static > img folder, the location of the favicon in the Django project.

What is favicon ico file?

The favicon. ico file is a small graphic icon that is used by some browsers (including Microsoft Internet Explorer and Mozilla Firefox) to enhance the display of address bar information and "favorites" bookmark lists. When requesting a resource, these browsers also try to locate the site's custom favicon.

What size should a favicon be?

Favicon images are small in size, only 16 pixels in height by 16 pixels in width, so there is not much space for complex designs. Still, a good favicon that is clean, simple and easily identifiable can provide a good visual indicator for visitors navigating to your site through their tabs or bookmarks.

How do I show favicon in HTML?

To add a favicon to your website, either save your favicon image to the root directory of your webserver, or create a folder in the root directory called images, and save your favicon image in this folder. A common name for a favicon image is "favicon.ico".


2 Answers

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")
like image 52
dm03514 Avatar answered Nov 15 '22 23:11

dm03514


This worked for me:

from django.conf.urls.static import static

...

if settings.DEBUG:
    urlpatterns += static(r'/favicon.ico', document_root='static/favicon.ico')
like image 25
Rafa He So Avatar answered Nov 15 '22 23:11

Rafa He So