I am implementing a duplicate vote checking. I set a cookie in the vote view:
# 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)
Now I want to access the cookie and than set a context variable in a generic details view. Is this possible or do I have to write a not generic one?
The resultion was to override 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