1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

newforms-admin: AdminSite index and display_login_form method can now take an optional extra_context argument, allowing you to inject extra template variables in to them from an over-ridden method on a subclass

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7631 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Simon Willison 2008-06-13 17:26:30 +00:00
parent 725293d51a
commit faae7c0faf
3 changed files with 29 additions and 9 deletions

View File

@ -249,7 +249,7 @@ class AdminSite(object):
else: else:
return self.display_login_form(request, ERROR_MESSAGE) return self.display_login_form(request, ERROR_MESSAGE)
def index(self, request): def index(self, request, extra_context=None):
""" """
Displays the main admin index page, which lists all of the installed Displays the main admin index page, which lists all of the installed
apps that have been registered in this site. apps that have been registered in this site.
@ -292,12 +292,16 @@ class AdminSite(object):
for app in app_list: for app in app_list:
app['models'].sort(lambda x, y: cmp(x['name'], y['name'])) app['models'].sort(lambda x, y: cmp(x['name'], y['name']))
return render_to_response(self.index_template or 'admin/index.html', { context = {
'title': _('Site administration'), 'title': _('Site administration'),
'app_list': app_list, 'app_list': app_list,
}, context_instance=template.RequestContext(request)) }
context.update(extra_context or {})
return render_to_response(self.index_template or 'admin/index.html', context,
context_instance=template.RequestContext(request)
)
def display_login_form(self, request, error_message=''): def display_login_form(self, request, error_message='', extra_context=None):
request.session.set_test_cookie() request.session.set_test_cookie()
if request.POST and request.POST.has_key('post_data'): if request.POST and request.POST.has_key('post_data'):
# User has failed login BUT has previously saved post data. # User has failed login BUT has previously saved post data.
@ -307,12 +311,17 @@ class AdminSite(object):
post_data = _encode_post_data(request.POST) post_data = _encode_post_data(request.POST)
else: else:
post_data = _encode_post_data({}) post_data = _encode_post_data({})
return render_to_response(self.login_template or 'admin/login.html', {
context = {
'title': _('Log in'), 'title': _('Log in'),
'app_path': request.path, 'app_path': request.path,
'post_data': post_data, 'post_data': post_data,
'error_message': error_message 'error_message': error_message
}, context_instance=template.RequestContext(request)) }
context.update(extra_context or {})
return render_to_response(self.login_template or 'admin/login.html', context,
context_instance=template.RequestContext(request)
)
# This global object represents the default admin site, for the common case. # This global object represents the default admin site, for the common case.

View File

@ -237,7 +237,18 @@ class AdminViewPermissionsTest(TestCase):
self.assertTemplateUsed(request, 'custom_admin/index.html') self.assertTemplateUsed(request, 'custom_admin/index.html')
self.assert_('Hello from a custom index template' in request.content) self.assert_('Hello from a custom index template' in request.content)
# Finally, using monkey patching check we can inject custom_context arguments in to index
original_index = admin.site.index
def index(*args, **kwargs):
kwargs['extra_context'] = {'foo': '*bar*'}
return original_index(*args, **kwargs)
admin.site.index = index
request = self.client.get('/test_admin/admin/')
self.assertTemplateUsed(request, 'custom_admin/index.html')
self.assert_('Hello from a custom index template *bar*' in request.content)
self.client.get('/test_admin/admin/logout/') self.client.get('/test_admin/admin/logout/')
del admin.site.index # Resets to using the original
admin.site.login_template = None admin.site.login_template = None
admin.site.index_template = None admin.site.index_template = None

View File

@ -1,6 +1,6 @@
{% extends "admin/index.html" %} {% extends "admin/index.html" %}
{% block content %} {% block content %}
Hello from a custom index template Hello from a custom index template {{ foo }}
{{ block.super }} {{ block.super }}
{% endblock %} {% endblock %}