Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #17515 -- Added ability to override the template of custom admi…

…n FilterSpec classes. Thanks, saxix and Julien Phalip.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17483 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4dbeb4bca4638ff851a2f4844d262dbe1652f7b5 1 parent 875a5ea
Jannis Leidel jezdez authored
1  django/contrib/admin/filters.py
View
@@ -17,6 +17,7 @@
class ListFilter(object):
title = None # Human-readable title to appear in the right sidebar.
+ template = 'admin/filter.html'
def __init__(self, request, params, model, model_admin):
# This dictionary will eventually contain the request's query string
12 django/contrib/admin/templatetags/admin_list.py
View
@@ -13,7 +13,8 @@
from django.utils.translation import ugettext as _
from django.utils.encoding import smart_unicode, force_unicode
from django.template import Library
-
+from django.template.loader import get_template
+from django.template.context import Context
register = Library()
@@ -360,9 +361,14 @@ def search_form(cl):
'search_var': SEARCH_VAR
}
-@register.inclusion_tag('admin/filter.html')
+@register.simple_tag
def admin_list_filter(cl, spec):
- return {'title': spec.title, 'choices' : list(spec.choices(cl))}
+ tpl = get_template(spec.template)
+ return tpl.render(Context({
+ 'title': spec.title,
+ 'choices' : list(spec.choices(cl)),
+ 'spec': spec,
+ }))
@register.inclusion_tag('admin/actions.html', takes_context=True)
def admin_actions(context):
14 docs/ref/contrib/admin/index.txt
View
@@ -697,8 +697,18 @@ subclass::
.. note::
- The ``FieldListFilter`` API is currently considered internal
- and prone to refactoring.
+ The ``FieldListFilter`` API is currently considered internal and prone
+ to refactoring.
+
+ .. versionadded:: 1.4
+
+ It is possible to specify a custom template for rendering a list filter::
+
+ class FilterWithCustomTemplate(SimpleListFilter):
+ template = "custom_template.html"
+
+ See the default template provided by django (``admin/filter.html``) for
+ a concrete example.
.. attribute:: ModelAdmin.list_max_show_all
10 tests/regressiontests/admin_views/admin.py
View
@@ -13,6 +13,7 @@
from django.db import models
from django.forms.models import BaseModelFormSet
from django.http import HttpResponse
+from django.contrib.admin import BooleanFieldListFilter
from .models import (Article, Chapter, Account, Media, Child, Parent, Picture,
Widget, DooHickey, Grommet, Whatsit, FancyDoodad, Category, Link,
@@ -25,7 +26,7 @@
CoverLetter, Story, OtherStory, Book, Promo, ChapterXtra1, Pizza, Topping,
Album, Question, Answer, ComplexSortedPerson, PrePopulatedPostLargeSlug,
AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
- AdminOrderedCallable, Report)
+ AdminOrderedCallable, Report, Color2)
def callable_year(dt_value):
@@ -525,6 +526,12 @@ def get_urls(self):
)
+class CustomTemplateBooleanFieldListFilter(BooleanFieldListFilter):
+ template = 'custom_filter_template.html'
+
+class CustomTemplateFilterColorAdmin(admin.ModelAdmin):
+ list_filter = (('warm', CustomTemplateBooleanFieldListFilter),)
+
site = admin.AdminSite(name="admin")
site.register(Article, ArticleAdmin)
site.register(CustomArticle, CustomArticleAdmin)
@@ -594,6 +601,7 @@ def get_urls(self):
site.register(AdminOrderedModelMethod, AdminOrderedModelMethodAdmin)
site.register(AdminOrderedAdminMethod, AdminOrderedAdminMethodAdmin)
site.register(AdminOrderedCallable, AdminOrderedCallableAdmin)
+site.register(Color2, CustomTemplateFilterColorAdmin)
# Register core models we need in our tests
from django.contrib.auth.models import User, Group
4 tests/regressiontests/admin_views/models.py
View
@@ -105,6 +105,10 @@ class Color(models.Model):
def __unicode__(self):
return self.value
+# we replicate Color to register with another ModelAdmin
+class Color2(Color):
+ class Meta:
+ proxy = True
class Thing(models.Model):
title = models.CharField(max_length=20)
7 tests/regressiontests/admin_views/templates/custom_filter_template.html
View
@@ -0,0 +1,7 @@
+<h3>By {{ filter_title }} (custom)</h3>
+<ul>
+{% for choice in choices %}
+ <li{% if choice.selected %} class="selected"{% endif %}>
+ <a href="{{ choice.query_string|iriencode }}">{{ choice.display }}</a></li>
+{% endfor %}
+</ul>
13 tests/regressiontests/admin_views/tests.py
View
@@ -1,6 +1,7 @@
# coding: utf-8
from __future__ import with_statement, absolute_import
+import os
import re
import datetime
import urlparse
@@ -593,6 +594,18 @@ def testChangeFormUrlHasCorrectValue(self):
self.assertTrue('form_url' in response.context, msg='form_url not present in response.context')
self.assertEqual(response.context['form_url'], 'pony')
+ def test_filter_with_custom_template(self):
+ """
+ Ensure that one can use a custom template to render an admin filter.
+ Refs #17515.
+ """
+ template_dirs = settings.TEMPLATE_DIRS + (
+ os.path.join(os.path.dirname(__file__), 'templates'),)
+ with self.settings(TEMPLATE_DIRS=template_dirs):
+ response = self.client.get("/test_admin/admin/admin_views/color2/")
+ self.assertTrue('custom_filter_template.html' in [t.name for t in response.templates])
+
+
class AdminJavaScriptTest(AdminViewBasicTest):
urls = "regressiontests.admin_views.urls"
Please sign in to comment.
Something went wrong with that request. Please try again.