上下文处理器只在服务器重启时更新

上下文处理器只在服务器重启时更新

问题描述:

我有一个数据库驱动的上下文处理器。上下文处理器只在服务器重启时更新

def on_air(request):    
    on_air = OnAir()    
    return {      
     'on_air': {    
      'now': on_air.now(), 
      'next': on_air.next(), 
     },       
    } 

编辑:这里是OnAir公司类

from collections import defaultdict 

from datetime import datetime, time 

from .models import ShowPage 


def schedule(): 
    schedule = defaultdict(list) 
    shows = ShowPage.objects.order_by(
     'weekday_start', 
     'saturday_start', 
     'sunday_start').all() 

    for show in shows: 
     for day in ('weekday', 'saturday', 'sunday',): 
      if getattr(show, '{day}_start'.format(day=day)): 
       schedule[day].append({ 
        'show': show, 
        'start': getattr(show, '{day}_start'.format(day=day)), 
        'end': getattr(show, '{day}_end'.format(day=day)), 
       }) 

    return schedule 


class OnAir: 

    def __init__(self, schedule=schedule()): 
     self.schedule = schedule 
     self.isoweekday = datetime.now().isoweekday() 
     self.current_time = datetime.now().time() 

    def now(self): 
     shows = [item for item in self.schedule[self._today()] 
       if item['start'] <= self.current_time 
       and item['end'] > self.current_time] 

     val = None 
     if shows: 
      val = shows[0] 
     else: 
      if self.current_time > time(12, 0): 
       # If no shows are found and it is after midday, 
       # then it means that the last show wraps over into the new day, 
       # eg. from 11pm to 1am the next morning 
       val = self._last_show_of_the_day(self._today()) 
      else: 
       # It is the crack of dawn and yesterday's last show 
       # started at say 11pm and carried through to say 3am today 
       val = self._last_show_of_the_day(self._yesterday()) 

     return val['show'] if val else None 

    def next(self): 
     shows = [item for item in self.schedule[self._today()] 
       if item['start'] > self.current_time] 

     val = None 
     if shows: 
      val = shows[0] 
     else: 
      val = self._first_show_of_the_day(self._tomorrow()) 

     return val['show'] if val else None 

    def _isoweekday_to_key(self, isoweekday): 
     if isoweekday == 0: 
      isoweekday = 7 

     if isoweekday == 8: 
      isoweekday = 1 

     val = 'weekday' 

     if isoweekday == 6: 
      val = 'saturday' 

     if isoweekday == 7: 
      val = 'sunday' 

     return val 

    def _today(self): 
     return self._isoweekday_to_key(self.isoweekday) 

    def _yesterday(self): 
     return self._isoweekday_to_key(self.isoweekday - 1) 

    def _tomorrow(self): 
     return self._isoweekday_to_key(self.isoweekday + 1) 

    def _first_show_of_the_day(self, today): 
     today = self.schedule[today] 
     return today[0] if today else None 

    def _last_show_of_the_day(self, today): 
     today = self.schedule[today] 
     return today[-1] if today else None 

如果我更新数据库,并刷新页面,没有什么变化。我必须重新启动dev服务器才能显示我的更改。

我该如何解决这个问题?

+1

您所显示的代码中没有任何内容显然会在请求之间保存状态。你需要显示你的'OnAir'类。 – Alasdair

+0

我分享了OnAir课程...... – Lee

问题在于OnAir类的__init__方法。

class OnAir: 
    def __init__(self, schedule=schedule()): 

schedule()当模块被加载,而不是因为你预期的OnAir实例被创建每次计算一次。

您可以通过将默认值更改为None,然后在__init__方法中创建计划来解决此问题。

class OnAir: 
    def __init__(self, schedule=None): 
     if schedule is None: 
      schedule = schedule() 
     ...