Ticket #11416: 11416-admin-caching.2.diff
File 11416-admin-caching.2.diff, 4.1 KB (added by , 15 years ago) |
---|
-
django/contrib/admin/sites.py
182 182 if not self.has_permission(request): 183 183 return self.login(request) 184 184 return view(request, *args, **kwargs) 185 return update_wrapper(inner, view)185 return never_cache(update_wrapper(inner, view)) 186 186 187 187 def get_urls(self): 188 188 from django.conf.urls.defaults import patterns, url, include -
tests/regressiontests/admin_views/tests.py
54 54 response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit) 55 55 self.failUnlessEqual(response.status_code, 200) 56 56 57 def testMaxAgeModelAdminView(self): 58 """ 59 Because cache time can be set by middleware, ensure max-age is explicity 0 60 (non-model-specific view) 61 """ 62 response = self.client.get('/test_admin/%s/admin_views/' % self.urlbit) 63 64 from django.utils.cache import get_max_age 65 self.failUnlessEqual(get_max_age(response), 0) 66 67 def testMaxAgeModelView(self): 68 """ 69 Because cache time can be set by middleware, ensure max-age is explicity 0 70 (model-specific view) 71 """ 72 response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit) 73 74 from django.utils.cache import get_max_age 75 self.failUnlessEqual(get_max_age(response), 0) 76 57 77 def testAddWithGETArgs(self): 58 78 response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit, {'name': 'My Section'}) 59 79 self.failUnlessEqual(response.status_code, 200) -
docs/ref/contrib/admin/index.txt
757 757 758 758 .. note:: 759 759 760 Not ice that the custom patterns are included*before* the regular admin760 Note how we included our custom patterns *before* the regular admin 761 761 URLs: the admin URL patterns are very permissive and will match nearly 762 762 anything, so you'll usually want to prepend your custom URLs to the built-in 763 763 ones. 764 764 765 Note, however, that the ``self.my_view`` function registered above will *not* 766 have any permission check done; it'll be accessible to the general public. Since 767 this is usually not what you want, Django provides a convience wrapper to check 768 permissions. This wrapper is :meth:`AdminSite.admin_view` (i.e. 769 ``self.admin_site.admin_view`` inside a ``ModelAdmin`` instance); use it like 770 so:: 765 However, the ``self.my_view`` function registered above suffers from two 766 problems: 771 767 768 * It will *not* have any permission check done; it'll be accessible to the 769 general public. 770 * It is not being marked as a non-cacheable and so, if it gets data from the 771 database, it could show outdated information because of content caching being 772 applied when the caching middleware is active. 773 774 Since this is usually not what you want, Django provides a convenience wrapper 775 to check permissions and mark the view as non-cacheable. This wrapper is 776 :meth:`AdminSite.admin_view` (i.e. ``self.admin_site.admin_view`` inside a 777 ``ModelAdmin`` instance); use it like so:: 778 772 779 class MyModelAdmin(admin.ModelAdmin): 773 780 def get_urls(self): 774 781 urls = super(MyModelAdmin, self).get_urls() … … 781 788 782 789 (r'^my_view/$', self.admin_site.admin_view(self.my_view)) 783 790 784 This wrapping will protect ``self.my_view`` from unauthorized access. 791 This wrapping will protect ``self.my_view`` from unauthorized access and will 792 apply the ``django.views.decorators.cache.never_cache`` decorator to make sure 793 it is not cached if the cache middleware is active. 785 794 786 795 .. method:: ModelAdmin.formfield_for_foreignkey(self, db_field, request, **kwargs) 787 796