Forums

using caching in urls.py

Hi, I got some errors while using cache in the URL. Could not find out what is the problem is-

from django.urls import path, re_path
from blog.views import PostCreateView, PostUpdateView, PostDeleteView
from . import views
urlpatterns = [
    path('', cache_page(60 * 15)PostListView.as_view(), name='blog-home'),
 ]

And in settings.py-

CACHES = {
'default': {
    'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
    'LOCATION': '/dev/shm',
    'TIMEOUT': 60,
    'OPTIONS': {
        'MAX_ENTRIES': 1000
    }
  }
}

What is the error you are getting? You need to pass your view function as an argument to the function returned by cache_page, so it's cache_page(60 * 15)(PostListView.as_view()) See https://docs.djangoproject.com/en/3.2/topics/cache/#specifying-per-view-cache-in-the-urlconf

Its says undefined name 'cache_page'

You need to import it from django.views.decorators.cache import cache_page

Thank you, it's working now.

Glad to hear that!