不确定这是django问题还是仅仅是python问题。我试图通过接受返回多个值的函数的值来获取缓存。
目前该页面在heavy_view函数上给出了这个错误:“'NoneType'对象不可迭代”
有没有让“counter,college_user,name_college_list = cache.get(cache_key)”行正常工作?然后检查它是否为空。谢谢!
#Views.py
def filter_results():
#some code here
return counter, college_user, name_college_List
def heavy_view(request):
cache_key = 'facebookcache'
cache_time = 180 # time to live in seconds
counter, college_user, name_college_list = cache.get(cache_key)
if not cache.get(cache_key):
result = filter_results() # some calculations here
cache.set(cache_key, result, cache_time)
return result
答案 0 :(得分:0)
这是一个Python的东西
当你做
时counter, college_user, name_college_list = cache.get(cache_key)
第一次没有价值 - >返回None
。
这有效吗?
result = cache.get(cache_key)
if result is None:
result = filter_results()
cache.set(cache_key, result, cache_time)
else:
counter, college_user, name_college_list = result
return result
答案 1 :(得分:0)
您可以这样做:
result = cache.get(cache_key)
if not result:
result = filter_result()
cache.set(cache_key, result, cache_time)
counter, college_user, name_college_list = result