2017-02-09 68 views
0

我正在實施重複投票檢查。我的投票視圖設置cookie:獲取Cookie並設置上下文通用視圖

# Set duplicate vote cookie. 
half_year = timedelta(weeks=26) 
expires = datetime.utcnow() + half_year 
if cookie and re.match(cookie_pattern, cookie): 
    redirect.set_cookie(cookie_name, "{}-{}".format(cookie, question.id), expires=expires) 
else: 
    redirect.set_cookie(cookie_name, question.id, expires=expires) 

現在我要訪問的餅乾,比一個普通的細節設置一個情境變量查看。這是可能的還是我必須寫一個不通用的?

回答

0

的resultion是覆蓋getcontextobject:

def get_context_data(self, **kwargs): 
    context = super().get_context_data(**kwargs) 
    # Check duplicate vote cookie 
    cookie = self.request.COOKIES.get(cookie_name) 
    if has_voted(cookie, self.object.id): 
     context['voted'] = True 
    return context