如何从djang的dir发送图像

2024-10-06 13:25:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为favicon添加一个类似/favicon.ico的url。我的图片位于static/graphics/images/favicon.ico。我不想改变方向。我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")
]

但代码给出了一个重定向。我想从dir加载图像而不重定向


Tags: pathdjangonamefromimporturl图片static
1条回答
网友
1楼 · 发布于 2024-10-06 13:25:21

您可以使用django smart\u 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

相关问题 更多 >