1. 程式人生 > >關於django2.0的favicon.ico和media的配置

關於django2.0的favicon.ico和media的配置

在settings.py中

MEDIA_URL = '/media/'
TEMPLATES[0]['OPTIONS']['context_processors'].append('django.template.context_processors.media')

在urls.py中

from django.urls import re_path
from django.conf import settings
from django.views.static import serve
from django.views.generic import RedirectView
from django.contrib.auth.decorators import login_required

urlpatterns = []
urlpatterns += [
    re_path(r'^favicon\.ico$', RedirectView.as_view(url='/static/images/favicon.ico')),
    re_path(r'^media/(?P<path>.*)$', login_required(serve), kwargs={'document_root': settings.MEDIA_ROOT}),
]

新增favicon.ico的目的是解決favicon.ico找不到報404錯誤的問題。