Fixed #8933 - Allow more admin templates to be overridden.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12217 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-01-12 23:34:46 +00:00
parent 31f3a8c1ad
commit a205691979
7 changed files with 75 additions and 11 deletions

View File

@ -37,8 +37,11 @@ class AdminSite(object):
"""
index_template = None
login_template = None
app_index_template = None
login_template = None
logout_template = None
password_change_template = None
password_change_done_template = None
def __init__(self, name=None, app_name='admin'):
self._registry = {} # model_class class -> admin_class instance
@ -248,14 +251,22 @@ class AdminSite(object):
url = '%spassword_change/done/' % self.root_path
else:
url = reverse('admin:password_change_done', current_app=self.name)
return password_change(request, post_change_redirect=url)
defaults = {
'post_change_redirect': url
}
if self.password_change_template is not None:
defaults['template_name'] = self.password_change_template
return password_change(request, **defaults)
def password_change_done(self, request):
"""
Displays the "success" page after a password change.
"""
from django.contrib.auth.views import password_change_done
return password_change_done(request)
defaults = {}
if self.password_change_done_template is not None:
defaults['template_name'] = self.password_change_done_template
return password_change_done(request, **defaults)
def i18n_javascript(self, request):
"""
@ -277,7 +288,10 @@ class AdminSite(object):
This should *not* assume the user is already logged in.
"""
from django.contrib.auth.views import logout
return logout(request)
defaults = {}
if self.logout_template is not None:
defaults['template_name'] = self.logout_template
return logout(request, **defaults)
logout = never_cache(logout)
def login(self, request):

View File

@ -1343,9 +1343,10 @@ and 500 pages.
Root and login templates
------------------------
If you wish to change the index or login templates, you are better off creating
your own ``AdminSite`` instance (see below), and changing the :attr:`AdminSite.index_template`
or :attr:`AdminSite.login_template` properties.
If you wish to change the index, login or logout templates, you are better off
creating your own ``AdminSite`` instance (see below), and changing the
:attr:`AdminSite.index_template` , :attr:`AdminSite.login_template` or
:attr:`AdminSite.logout_template` properties.
``AdminSite`` objects
=====================
@ -1375,17 +1376,30 @@ provided, a default instance name of ``admin`` will be used.
``AdminSite`` attributes
------------------------
Templates can override or extend base admin templates as described in
`Overriding Admin Templates`_.
.. attribute:: AdminSite.index_template
Path to a custom template that will be used by the admin site main index view.
Templates can override or extend base admin templates as described in
`Overriding Admin Templates`_.
.. attribute:: AdminSite.login_template
Path to a custom template that will be used by the admin site login view.
Templates can override or extend base admin templates as described in
`Overriding Admin Templates`_.
.. attribute:: AdminSite.logout_template
Path to a custom template that will be used by the admin site logout view.
.. attribute:: AdminSite.password_change_template
Path to a custom template that will be used by the admin site password change
view.
.. attribute:: AdminSite.password_change_done_template
Path to a custom template that will be used by the admin site password change
done view.
Hooking ``AdminSite`` instances into your URLconf
-------------------------------------------------

View File

@ -9,7 +9,10 @@ import models
class Admin2(admin.AdminSite):
login_template = 'custom_admin/login.html'
logout_template = 'custom_admin/logout.html'
index_template = 'custom_admin/index.html'
password_change_template = 'custom_admin/password_change_form.html'
password_change_done_template = 'custom_admin/password_change_done.html'
# A custom index view.
def index(self, request, extra_context=None):

View File

@ -286,11 +286,26 @@ class CustomModelAdminTest(AdminViewBasicTest):
self.assertTemplateUsed(request, 'custom_admin/login.html')
self.assert_('Hello from a custom login template' in request.content)
def testCustomAdminSiteLogoutTemplate(self):
request = self.client.get('/test_admin/admin2/logout/')
self.assertTemplateUsed(request, 'custom_admin/logout.html')
self.assert_('Hello from a custom logout template' in request.content)
def testCustomAdminSiteIndexViewAndTemplate(self):
request = self.client.get('/test_admin/admin2/')
self.assertTemplateUsed(request, 'custom_admin/index.html')
self.assert_('Hello from a custom index template *bar*' in request.content)
def testCustomAdminSitePasswordChangeTemplate(self):
request = self.client.get('/test_admin/admin2/password_change/')
self.assertTemplateUsed(request, 'custom_admin/password_change_form.html')
self.assert_('Hello from a custom password change form template' in request.content)
def testCustomAdminSitePasswordChangeDoneTemplate(self):
request = self.client.get('/test_admin/admin2/password_change/done/')
self.assertTemplateUsed(request, 'custom_admin/password_change_done.html')
self.assert_('Hello from a custom password change done template' in request.content)
def testCustomAdminSiteView(self):
self.client.login(username='super', password='secret')
response = self.client.get('/test_admin/%s/my_view/' % self.urlbit)

View File

@ -0,0 +1,6 @@
{% extends "registration/logged_out.html" %}
{% block content %}
Hello from a custom logout template
{{ block.super }}
{% endblock %}

View File

@ -0,0 +1,6 @@
{% extends "registration/password_change_done.html" %}
{% block content %}
Hello from a custom password change done template
{{ block.super }}
{% endblock %}

View File

@ -0,0 +1,6 @@
{% extends "registration/password_change_form.html" %}
{% block content %}
Hello from a custom password change form template
{{ block.super }}
{% endblock %}