我已经将json从api导入到我的视图中。它现在工作得很好,但不知何故,如果我进入网站,不知何故django缓存了api给出的值,因此不显示最近的值。我尝试过使用"never_cache“和"cache control”,但都不起作用。有没有可以用django或Apache做的解决方案?
我的观点
from __future__ import unicode_literals
from django.shortcuts import render
from urllib import urlopen
import json
from django.views.decorators.cache import never_cache, cache_control
response = json.loads(urlopen('http://api.fixer.io/latest').read())
usdollar = response['rates']['USD']
#@never_cache
@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
def home(request):
return render(request, "index.html", {"usdollar":usdollar})发布于 2017-08-22 23:37:17
目前,您正在进行模块级的API调用。这意味着它只会在模块加载时运行一次。您应该将代码移动到视图中:
#@never_cache
@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
def home(request):
response = json.loads(urlopen('http://api.fixer.io/latest').read())
usdollar = response['rates']['USD']
return render(request, "index.html", {"usdollar":usdollar})https://stackoverflow.com/questions/45821783
复制相似问题