Django

Code

Changeset 7631

Show
Ignore:
Timestamp:
06/13/08 12:26:30 (3 months ago)
Author:
simon
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/newforms-admin/django/contrib/admin/sites.py

    r7630 r7631  
    9999 
    100100        `url` is the remainder of the URL -- e.g. 'comments/comment/'. 
    101         """ 
     101        """         
    102102        url = url.rstrip('/') # Trim trailing slash, if it exists. 
    103103 
     
    250250                return self.display_login_form(request, ERROR_MESSAGE) 
    251251 
    252     def index(self, request): 
     252    def index(self, request, extra_context=None): 
    253253        """ 
    254254        Displays the main admin index page, which lists all of the installed 
     
    292292        for app in app_list: 
    293293            app['models'].sort(lambda x, y: cmp(x['name'], y['name'])) 
    294  
    295         return render_to_response(self.index_template or 'admin/index.html',
     294         
     295        context =
    296296            'title': _('Site administration'), 
    297297            'app_list': app_list, 
    298         }, context_instance=template.RequestContext(request)) 
    299  
    300     def display_login_form(self, request, error_message=''): 
     298        } 
     299        context.update(extra_context or {}) 
     300        return render_to_response(self.index_template or 'admin/index.html', context,  
     301            context_instance=template.RequestContext(request) 
     302        ) 
     303 
     304    def display_login_form(self, request, error_message='', extra_context=None): 
    301305        request.session.set_test_cookie() 
    302306        if request.POST and request.POST.has_key('post_data'): 
     
    308312        else: 
    309313            post_data = _encode_post_data({}) 
    310         return render_to_response(self.login_template or 'admin/login.html', { 
     314         
     315        context = { 
    311316            'title': _('Log in'), 
    312317            'app_path': request.path, 
    313318            'post_data': post_data, 
    314319            'error_message': error_message 
    315         }, context_instance=template.RequestContext(request)) 
     320        } 
     321        context.update(extra_context or {}) 
     322        return render_to_response(self.login_template or 'admin/login.html', context, 
     323            context_instance=template.RequestContext(request) 
     324        ) 
    316325 
    317326 
  • django/branches/newforms-admin/tests/regressiontests/admin_views/tests.py

    r7630 r7631  
    237237        self.assertTemplateUsed(request, 'custom_admin/index.html') 
    238238        self.assert_('Hello from a custom index template' in request.content) 
    239          
    240         self.client.get('/test_admin/admin/logout/') 
     239                 
     240        # Finally, using monkey patching check we can inject custom_context arguments in to index 
     241        original_index = admin.site.index 
     242        def index(*args, **kwargs): 
     243            kwargs['extra_context'] = {'foo': '*bar*'} 
     244            return original_index(*args, **kwargs) 
     245        admin.site.index = index 
     246        request = self.client.get('/test_admin/admin/') 
     247        self.assertTemplateUsed(request, 'custom_admin/index.html') 
     248        self.assert_('Hello from a custom index template *bar*' in request.content) 
     249         
     250        self.client.get('/test_admin/admin/logout/') 
     251        del admin.site.index # Resets to using the original 
    241252        admin.site.login_template = None 
    242253        admin.site.index_template = None 
  • django/branches/newforms-admin/tests/templates/custom_admin/index.html

    r7630 r7631  
    22 
    33{% block content %} 
    4 Hello from a custom index template 
     4Hello from a custom index template {{ foo }} 
    55{{ block.super }} 
    66{% endblock %}