I am new to Django and added a vote duplicate check to the tutorial application. I set a cookie in the vote view:cookie_name = 'voted'cookie_pattern = '^(\d-?)+$'# ...def vote(request, question_id):    question = get_object_or_404(Question, pk=question_id)    # ...    # 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)And then check for the cookie in the detail and vote view:def has_voted(cookie, question_id):    Checks for question_id in cookie    return cookie and \        re.match(cookie_pattern, cookie) and \        question_id in list(map(int, cookie.split('-')))class DetailView(generic.DetailView):    model = Question    template_name = 'polls/detail.html'    # ...    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 contextdef vote(request, question_id):    question = get_object_or_404(Question, pk=question_id)    cookie = request.COOKIES.get(cookie_name)    if has_voted(cookie, question.id):        return render(request, 'polls/detail.html', {            'question': question,            'voted': True,        })What can be improved in this implementation?
Duplicate vote checking
python;beginner;django