Is it possible to disable caching from Django admin pages?

django per-view cache
django cache
django clear cache
django cache redis
django caching best practices
django cache function result
django never_cache
django template fragment caching

[additional information]

I asked for a way to disable caching site-wide. This is probably overkill, because all I need is a way to be able to see the most recent version of a page, when either the database or the program to generate it has been modified.

There is a strong consensus that modifying settings at runtime is a very bad idea.

So, some ideas: clearing the cache could work, as would sending a flag to specify that I don't want to see a cached version, or specifying that requests from my IP address shouldn't see cached pages.

[original question]

I have a Django-based website at ozake.com, and I frequently rewrite parts of the programming or change page content.

Each time I work on it, I modify settings.py to disable caching so I can see my modifications in real time.

When I'm done, I re-enable caching.

I am using file-based caching. Here is the relevant part of settings.py:

CACHES = {
  'default': {'BACKEND':
 #'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
  'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
  'LOCATION': '/var/www/mysite.com/cache',

When I work on the site I comment out the last two lines and uncomment the dummy cache line.

This means SSH'ing into the site, modifying settings.py, working on the site, then re-modifying it.

Is there any way I can make this into a check box somewhere in /admin with admin.py?

If you just want to invalidate the whole cache at will, you might create a trivial view which does that:

file views.py:

from django.core.cache import cache
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.views.decorators.cache import never_cache

@never_cache
def clear_cache(request):
    if not request.user.is_superuser:
        raise PermissionDenied
    cache.clear()
    return HttpResponse('Cache has been cleared')

file urls.py:

from django.urls import path
from . import views

urlpatterns = [
    ...
    path('clear_cache/', views.clear_cache),
]

then invoke it with your browser:

http://HOST/clear_cache

Django's cache framework | Django documentation, Django comes with a robust cache system that lets you save dynamic pages so they install the cache model on primary" if app_label == 'django_cache': return db private cache instance, which means no cross-process caching is possible. decorator) or disable caching for a view (using the never_cache() decorator). Django comes with its own caching system that lets you save your dynamic pages, to avoid calculating them again when needed. The good point in Django Cache framework is that you can cache − The output of a specific view. A part of a template. Your entire site. To use cache in Django, first thing to do is to set up where the cache will stay.

What I ended up doing:

from django.core.cache import cache

def PageView(request):
    ...
    if request.GET.get('clear') == 'cache':
        if request.user.is_superuser:
            title = request.GET.get('flag') + ' ' + title 
            cache.clear()
    ...
    return render(request, template, context)

So now I can go to https://ozake.com/en/home?clear=cache&flag=sdf

And the cache will be cleared (if I am logged in to the admin pages).

Two notes:

  1. This address will be cached, so it is necessary to change the flag string each time, to clear the cache.

  2. The flag is added to the page title so you can be sure you are seeing the most recent version.

Settings | Django documentation, The model name used in this setting should be all lowercase, regardless of the case The default number of seconds to cache a page for the cache middleware . The simplest possible settings file is for a single-database setup using SQLite. Set this to False if you want to disable Django's transaction management and� Is it possible to disable django haystack for some tests? J2ME - using javax.microedition.amms.control.camera.CameraControl; is it possible to disable shutter sound? Is it possible to disable caching from Django admin pages? Is it possible to disable MISRA checks of external libraries? Disable and Enable Hover in AngularJS

13008 (@never_cache decorator should add 'no-cache' & 'no-store'), This does _not_ prevent browsers to actually cache that page. Oon the other hand I am probably a 'power user' who knows when I am likely to be Since we always re-direct after a POST in Django admin, we don't see those pages, and the� Upstream caching is a nice efficiency boost, but there’s a danger to it: Many Web pages’ contents differ based on authentication and a host of other variables, and cache systems that blindly save pages based purely on URLs could expose incorrect or sensitive data to subsequent visitors to those pages.

You can use the django-admin-caching module. First to install it type this in your bash terminal:

pip install django-admin-caching

After it is installed, you add this piece of code to your settings.py so that it auto-registers itself:

settings.INSTALLED_APPS = [
   #...
   'django_admin_caching',
   #...
]

This should enable caching on your django admin page.

How to manage caching — django cms 3.7.4 documentation, If you disable a plugin cache be sure to restart the server and clear the cache afterwards. Content Cache Duration�. Default: 60. This can be changed in� How to customize the admin actions in list pages of Django admin? Django by default provides automatic admin interface, that reads metadata from your models to provide a beautiful model interface where trusted users can manage content on your site.

For the main request, you can use cache-table.

An alternative is to use middleware comparing the IP that receives the request with your specific IP, that you can store in your settings. Something like this:

from django.http import HttpRequest
def get_user_ip(request):
    client_address = request.META['HTTPS_X_FORWARDED_FOR']
    if your_ip == client_address:
        save_user_ip()

Chapter 15: Caching — Django Book 0.1 documentation, Django comes with a robust cache system that lets you save dynamic pages so they private cache instance, which means no cross-process caching is possible. way of disabling caching for any user-specific pages (include Django's admin� Django admin custom page layout with custom templates. Although the django.contrib.admin.site object options presented in listing 11-22 offer a quick way to customize Django admin pages, they can fall short in the face of more sophisticated requirements, in which case you must rely on custom templates.

Django Caching - It's Easy if you do it in the Smart Way!, Django caching tutorial covers caching concept, setting up caching sytem, types The CPU will process the page and that time will add to the response time. Debian/ Ubuntu OS, some of you may not be able to install it on the system. Explore the Django Admin Interface – Setting up Django Admin Site� The Django admin changelist rows are not contained within a block that could be extended through standard template caching with tags. Also, the generation of the cache key for more complex objects might be too complicated to do in the templates.

Caching in Django, This article first provides an overview of Django's caching framework it's a good idea to implement a caching mechanism to prevent having to A table for that purpose can be created with one of the Django's admin commands. Rather than wasting precious memory space on caching static pages or� The Django admin changelist rows are not contained within a block that could be extended through standard template caching with tags. Also, the generation of the cache key for more complex objects might be too complicated to do in the templates.

Django project optimization guide (part 3), You can install django-redis package and configure it as a cache backend. If for example page content changes are based on authenticated user, This type of caching is possible only for methods without arguments (besides of self ). event-based by setting up model signals of corresponding models. You’ve learned some techniques for caching, including caching the whole site with Varnish or controlling the cache settings on a per-view basis. You’ve ensured static files are being served with the webserver, not Django. You’ve learned how to use uWSGI to boost your Django app’s performance by running it out of process from the webserver.

Comments
  • I am starting to think that I need to just maintain a separate site for development.
  • maybe place a script (shell) on the machine where your site is deployed, on a certain action that script gets executed and modifies the settings file?
  • Maybe using django-constance to set a dynamic boolean flag from the admin, then create a custom cache backend which behaves like DummyCache or FileBasedCache according to the flag value ?
  • Long shot, but github.com/jazzband/django-configurations gives you the opportunity to use environment variables as values in django settings. You could possibly create a button in the admin that changes the environment variable and restarts the server.
  • "I am starting to think that I need to just maintain a separate site for development."—You really, really should. Or, actually, you need to maintain a separate site for production, since your current "production" site is really a development site.
  • I will modify the solution slightly… the thought of having an unprotected address that affects caching makes me shiver. The technique is just what I needed though. Thanks so much.
  • You're welcome ! As a side note: the view is already protected against unauthorized access - if the user issuing the request is not a superuser, an exception is thrown and no caching is affected at all
  • So I could just add this to the regular default view, and super users would always get the most recent version. I don't need the URL. It's funny how much simpler things can become when you have more knowledge.
  • I understand your point ... but while this might seem convenient, you also loose a little bit of control. First, please note that when the superuser goes to the "default page" all users are affected, since the cache is invalidated system-wide. Second, you always get cache invalidation every time you invoke the default view (and never when you work with other views) even when unnecessary. This sounds much like an uncontrolled side effect. Even if it's not a big deal in this case, it not the safest solution either ...
  • ... I'm still convinced that a specific url for cache invalidation is more appropriate an effective. Open a new tab in the browser with that address, work with your site contents, and when you're done with editing refresh the "clear_cache page" ... it's that simple ;)
  • Obviously, the flag being added to the title is specific to my CMS program.
  • You can go one step further and place this url inside one of the ModelAdmins using get_urls() and wraping it in admin_view() so that this function is only accessible to logged in Admin users.
  • This looks like a solid solution but I don't have the time or expertise to put it to use right now. I'll come back to it when I'm further along in the overall project and need a more robust solution. I'm really very inexperienced at Django and anyone who looked at my project would probably cringe.
  • It looks like this is for caching the admin pages, not the main site. Am I mistaken?