我想覆盖默认的django管理过滤器模板,使用我自己的模板:
https://github.com/feincms/feincms/blob/master/feincms/templates/admin/filter.html
我通过继承SimpleListFilter
django.contrib.admin.SimpleListFilter
课程
class PublisherStateFilter(admin.SimpleListFilter):
title = _('Status')
parameter_name = 'status'
template = 'admin/blogitty/filter.html'
[...]
这很有效。
但是我想对所有管理过滤器使用相同的模板。有没有办法覆盖给定应用的所有过滤器模板,而无需为每个ListFilter
和ForeignKey
关系定义自定义ManyToMany
。
我的项目为blogitty
。我尝试了模板DIR的两个选项:
blogitty/templates/admin/filter.html
和
blogitty/templates/admin/blogitty/filter.html
没有运气: - (
浏览源代码:
https://github.com/django/django/blob/master/django/contrib/admin/options.py#L1030
return TemplateResponse(request, form_template or [
"admin/%s/%s/change_form.html" % (app_label, opts.model_name),
"admin/%s/change_form.html" % app_label,
"admin/change_form.html"
], context)
https://github.com/django/django/blob/master/django/contrib/admin/options.py#L1569
return TemplateResponse(request, self.change_list_template or [
'admin/%s/%s/change_list.html' % (app_label, opts.model_name),
'admin/%s/change_list.html' % app_label,
'admin/change_list.html'
], context)
据我所知。 Django ModelAdmin检查多个路径以呈现给定模型的changeform或changelist。但是对于ListFilter
,不会进行额外的检查来加载自定义模板。
https://github.com/django/django/blob/master/django/contrib/admin/filters.py#L60
class ListFilter(object):
title = None
template = 'admin/filter.html'
更新 - TEMPLATE_DIRS设置:
BASE_DIR = dirname(dirname(__file__))
TEMPLATE_DIRS = (
join(BASE_DIR, 'templates'),
)
项目布局基于Daniel Greenfeld的cookiecutter-django
答案 0 :(得分:2)
这可能会有所帮助
class ClassFilter1(admin.ModelAdmin):
title = 'Filter Class'
parameter_name = 'filter-class'
def lookups(self, request, model_admin):
# Your Lookups
def queryset(self, request, queryset):
# Your Lookups
class FilterClass(admin.ModelAdmin):
list_filter = (ClassFilter1, ClassFilter2)
change_list_template = 'polls/change_list_template.html'
并覆盖change_list_template.html
并将.html放在polls/templates/polls