我想为favicon添加一个网址,例如/favicon.ico。我的图像位于static / graphics / images / favicon.ico。我不想重定向。我试过:-
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")
]
但是代码给出了重定向。我想从目录加载图像而不进行重定向。
您可以使用django smart_str方法从目录发送图像。
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