0

I want to add a url for favicon like /favicon.ico. My image is at static/graphics/images/favicon.ico. I don't want to redirect. I tried:-

from django.contrib import admin
from django.urls import path, include
from search import views
from django.views.generic.base import RedirectView
faviconI=RedirectView.as_view(url='/static/gr/favicon.ico')
urlpatterns = [
    path('', views.Page, name='Home'),
    path('favicon.ico/', faviconI, name="favicon")
]

But the code gave a redirect. I want to load image from dir without redirecting.

Nawal Kishore
  • 159
  • 1
  • 9
  • >You might look at https://stackoverflow.com/questions/1156246/having-django-serve-downloadable-files and look at Cory's answer suggesting to use serve. so you have to create a view function / class that you reference in urlpatterns. The view function calls serve as mentioned in Cory's answer – gelonida Nov 30 '19 at 15:18

1 Answers1

0

You can use django smart_str method to send image from the directory.

from django.utils.encoding import smart_str
response = HttpResponse(mimetype='application/force-download')    
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['X-Sendfile'] = smart_str(path_to_file)
return response
Dhruv Rajkotia
  • 370
  • 2
  • 8