首页 > 解决方案 > 如何在 django 中获取视图功能之外的当前 URL

问题描述

我想在视图函数之外获取当前 url,以便我可以限制在特定 URL 处使用装饰器

@cache_page(CACHE_TTL)
def patients(request):
    baseContext = BaseContext(header="Dieter")
    return baseContext.render(request, "patients/patients.html")

现在我想在 URL 为“ https://example.com ”时使用这个“@cache_page”装饰器我该怎么做?

标签: pythondjangodjango-cache

解决方案


因此,您可以相应地在本地或生产设置文件中添加缓存配置,如下所示 -

#local_config.py
#https://docs.djangoproject.com/en/2.2/topics/cache/

CACHES = {
  'default': {
      'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
      'LOCATION': '127.0.0.1:11211',
  }
}

同样在产品配置中,

#prod_config.py
CACHES = {
  'default': {
      'BACKEND': 'mypackage.backends.whatever.WhateverCache',
      'LOCATION': 'redis://xx.xx.xx.xx:xx',
      'TIMEOUT': None,
   },
}

因此,您可以使用任何其他缓存,例如 FileBasedCache,而不是访问(使用)您的本地产品缓存服务。


推荐阅读