【发布时间】:2015-06-19 06:52:57
【问题描述】:
我遇到了一个奇怪的问题。这是我的看法:
def get_date_range(this_week=True, last_week=False, older_than_two_weeks=False):
dates = {}
d = datetime.today().strftime('%Y-%m-%d')
dt = datetime.strptime(d, '%Y-%m-%d')
if this_week:
this_week_start = dt - timedelta(days = dt.weekday())
this_week_end = this_week_start + timedelta(days=6)
elif (last_week or older_than_two_weeks):
this_week_start = dt - timedelta(days = 14)
this_week_end = this_week_start + timedelta(days=6)
dates = {'date_start' :this_week_start, 'date_end' : this_week_end}
return dates
def load_created_topics_by_time(request):
"""
created this week, last week, older
"""
objects={}
date_range = get_date_range(this_week=True)
topics_this_week = Topic.objects
.filter(is_active=True, date_created__range=(date_range['date_start'],date_range['date_end']))
.order_by('-date_created')
objects['topics_this_week'] = topics_this_week
date_range = get_date_range(this_week=False,last_week=True)
topics_last_week = Topic.objects.filter(is_active=True, date_created__range= (date_range['date_start'],date_range['date_end'])).order_by('-date_created')
objects['topics_last_week'] = topics_last_week
topics_older_two_weeks = Topic.objects.filter(is_active=True, date_created__lt=date_range['date_start']).order_by('-date_created')[:50]
objects['topics_older_two_weeks'] = topics_older_two_weeks
return shortcuts.render(request, 'template.html', objects)
现在,当我在我的机器上运行它时,它运行良好,但从服务器加载显然只命中了一次“get_date_range”。似乎该函数的结果正在被缓存,这当然会弄乱我的结果。
我不能使用@never_cache,因为它是一个函数而不是一个视图。有什么解决办法吗?
谢谢, 大卫
【问题讨论】:
-
这真的是您的确切代码吗?您确定从 load_created_topics_by_time 视图中调用 get_date_range 函数?
-
是的,它的确切代码。对函数的两次调用都在 load_created_topics_by_time 内
-
为什么不能使用
@never_cache?调用该函数的视图正在兑现,因此该函数仅被调用一次。 -
我已经尝试在 load_created_topics_by_time 视图上使用该装饰器,但仍然没有运气。
-
这可能是数据问题吗?数据存在本地,但不存在服务器上?
标签: python django django-cache