python - 如何在 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”装饰器我该怎么做?
解决方案
因此,您可以相应地在本地或生产设置文件中添加缓存配置,如下所示 -
#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,而不是访问(使用)您的本地产品缓存服务。
推荐阅读
- javascript - 页面加载后谷歌地图无法读取未定义的属性“扩展”
- flutter - 如何在颤动中限制文本字段中的单引号和双引号?
- reactjs - 如何为每个数据块添加按钮
- c# - 如何使用 HtmlAgilityPack 从网站获取单个图像?
- flutter - 如何在几个小部件上限定提供者而不将提供者放在根小部件上
- mysql - MYSQL 在使用双引号和单引号时截断不正确的 DOUBLE
- javascript - 如何返回我可以使用的 Base64
- python - 为不在烧瓶中工作的不同用户登录
- sql-server - 物化视图概念是否与 SQL Server 中的索引视图相同?
- reactjs - React 登录表单循环不会重新渲染 DOM