Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored
1  django/contrib/admin/filters.py
@@ -17,6 +17,7 @@
17 17
 
18 18
 class ListFilter(object):
19 19
     title = None  # Human-readable title to appear in the right sidebar.
  20
+    template = 'admin/filter.html'
20 21
 
21 22
     def __init__(self, request, params, model, model_admin):
22 23
         # This dictionary will eventually contain the request's query string
12  django/contrib/admin/templatetags/admin_list.py
@@ -13,7 +13,8 @@
13 13
 from django.utils.translation import ugettext as _
14 14
 from django.utils.encoding import smart_unicode, force_unicode
15 15
 from django.template import Library
16  
-
  16
+from django.template.loader import get_template
  17
+from django.template.context import Context
17 18
 
18 19
 register = Library()
19 20
 
@@ -360,9 +361,14 @@ def search_form(cl):
360 361
         'search_var': SEARCH_VAR
361 362
     }
362 363
 
363  
-@register.inclusion_tag('admin/filter.html')
  364
+@register.simple_tag
364 365
 def admin_list_filter(cl, spec):
365  
-    return {'title': spec.title, 'choices' : list(spec.choices(cl))}
  366
+    tpl = get_template(spec.template)
  367
+    return tpl.render(Context({
  368
+        'title': spec.title,
  369
+        'choices' : list(spec.choices(cl)),
  370
+        'spec': spec,
  371
+    }))
366 372
 
367 373
 @register.inclusion_tag('admin/actions.html', takes_context=True)
368 374
 def admin_actions(context):
14  docs/ref/contrib/admin/index.txt
@@ -697,8 +697,18 @@ subclass::
697 697
 
698 698
       .. note::
699 699
 
700  
-          The ``FieldListFilter`` API is currently considered internal
701  
-          and prone to refactoring.
  700
+        The ``FieldListFilter`` API is currently considered internal and prone
  701
+        to refactoring.
  702
+
  703
+    .. versionadded:: 1.4
  704
+
  705
+    It is possible to specify a custom template for rendering a list filter::
  706
+
  707
+        class FilterWithCustomTemplate(SimpleListFilter):
  708
+            template = "custom_template.html"
  709
+
  710
+    See the default template provided by django (``admin/filter.html``) for
  711
+    a concrete example.
702 712
 
703 713
 .. attribute:: ModelAdmin.list_max_show_all
704 714
 
10  tests/regressiontests/admin_views/admin.py
@@ -13,6 +13,7 @@
13 13
 from django.db import models
14 14
 from django.forms.models import BaseModelFormSet
15 15
 from django.http import HttpResponse
  16
+from django.contrib.admin import BooleanFieldListFilter
16 17
 
17 18
 from .models import (Article, Chapter, Account, Media, Child, Parent, Picture,
18 19
     Widget, DooHickey, Grommet, Whatsit, FancyDoodad, Category, Link,
@@ -25,7 +26,7 @@
25 26
     CoverLetter, Story, OtherStory, Book, Promo, ChapterXtra1, Pizza, Topping,
26 27
     Album, Question, Answer, ComplexSortedPerson, PrePopulatedPostLargeSlug,
27 28
     AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
28  
-    AdminOrderedCallable, Report)
  29
+    AdminOrderedCallable, Report, Color2)
29 30
 
30 31
 
31 32
 def callable_year(dt_value):
@@ -525,6 +526,12 @@ def get_urls(self):
525 526
         )
526 527
 
527 528
 
  529
+class CustomTemplateBooleanFieldListFilter(BooleanFieldListFilter):
  530
+    template = 'custom_filter_template.html'
  531
+
  532
+class CustomTemplateFilterColorAdmin(admin.ModelAdmin):
  533
+    list_filter = (('warm', CustomTemplateBooleanFieldListFilter),)
  534
+
528 535
 site = admin.AdminSite(name="admin")
529 536
 site.register(Article, ArticleAdmin)
530 537
 site.register(CustomArticle, CustomArticleAdmin)
@@ -594,6 +601,7 @@ def get_urls(self):
594 601
 site.register(AdminOrderedModelMethod, AdminOrderedModelMethodAdmin)
595 602
 site.register(AdminOrderedAdminMethod, AdminOrderedAdminMethodAdmin)
596 603
 site.register(AdminOrderedCallable, AdminOrderedCallableAdmin)
  604
+site.register(Color2, CustomTemplateFilterColorAdmin)
597 605
 
598 606
 # Register core models we need in our tests
599 607
 from django.contrib.auth.models import User, Group
4  tests/regressiontests/admin_views/models.py
@@ -105,6 +105,10 @@ class Color(models.Model):
105 105
     def __unicode__(self):
106 106
         return self.value
107 107
 
  108
+# we replicate Color to register with another ModelAdmin
  109
+class Color2(Color):
  110
+    class Meta:
  111
+        proxy = True
108 112
 
109 113
 class Thing(models.Model):
110 114
     title = models.CharField(max_length=20)
7  tests/regressiontests/admin_views/templates/custom_filter_template.html
... ...
@@ -0,0 +1,7 @@
  1
+<h3>By {{ filter_title }} (custom)</h3>
  2
+<ul>
  3
+{% for choice in choices %}
  4
+    <li{% if choice.selected %} class="selected"{% endif %}>
  5
+    <a href="{{ choice.query_string|iriencode }}">{{ choice.display }}</a></li>
  6
+{% endfor %}
  7
+</ul>
13  tests/regressiontests/admin_views/tests.py
... ...
@@ -1,6 +1,7 @@
1 1
 # coding: utf-8
2 2
 from __future__ import with_statement, absolute_import
3 3
 
  4
+import os
4 5
 import re
5 6
 import datetime
6 7
 import urlparse
@@ -593,6 +594,18 @@ def testChangeFormUrlHasCorrectValue(self):
593 594
         self.assertTrue('form_url' in response.context, msg='form_url not present in response.context')
594 595
         self.assertEqual(response.context['form_url'], 'pony')
595 596
 
  597
+    def test_filter_with_custom_template(self):
  598
+        """
  599
+        Ensure that one can use a custom template to render an admin filter.
  600
+        Refs #17515.
  601
+        """
  602
+        template_dirs = settings.TEMPLATE_DIRS + (
  603
+            os.path.join(os.path.dirname(__file__), 'templates'),)
  604
+        with self.settings(TEMPLATE_DIRS=template_dirs):
  605
+            response = self.client.get("/test_admin/admin/admin_views/color2/")
  606
+            self.assertTrue('custom_filter_template.html' in [t.name for t in response.templates])
  607
+
  608
+
596 609
 class AdminJavaScriptTest(AdminViewBasicTest):
597 610
     urls = "regressiontests.admin_views.urls"
598 611
 

0 notes on commit 4dbeb4b

Please sign in to comment.
Something went wrong with that request. Please try again.