Skip to content

Django cache per view #146

@dalescher

Description

@dalescher

I am trying to use locMemCache to cache some views with Django. When I cache the whole site with the following settings for middleware, everything works as expected, I have both html minification and caching working properly:

MIDDLEWARE = [
    'django.middleware.cache.UpdateCacheMiddleware',
    'htmlmin.middleware.HtmlMinifyMiddleware',
    [...]
    'django.middleware.cache.FetchFromCacheMiddleware',
    'htmlmin.middleware.MarkRequestMiddleware',
]

However, when I try to cache only some specific views, I delete both UpdateCacheMiddleware and FetchFromCacheMiddleware, and use instead the following in urls.py (as per django documentation about cache framework):

from django.views.decorators.cache import cache_page

urlpatterns = [
    path('', cache_page(600)(views.HomePage.as_view()), name='home'),
]

Then, if I do not use htmlmin (HTML_MINIFY = False), caching is working fine. But if I activate htmlmin, caching is not working anymore. I tried to move htmlmin.middleware.HtmlMinifyMiddleware and htmlmin.middleware.MarkRequestMiddleware at different locations in the MIDDLEWARE setting, beginning, end, but without any effect. I also tried to use only one of the two htmlmin middlewares, but then there is no minification.

What am I missing here? Is it possible to use htmlmin with per-view caching?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions