'How Can I change "vote" view to put into class in django views?

I'm in the process of making a poll app in django. I'm having trouble with changing my "vote" view into class. I'm asking someone for help with this. Below I show a part of my code in views, models and urls python file and some template.

polls/views.py:

from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic
from django.utils import timezone
from .models import Choice, Question, Comment

#other view

class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'

    def get_queryset(self):
        return Question.objects.filter(pub_date__lte=timezone.now())
    #...

#other view

def vote(request, slug):
    question = get_object_or_404(Question, slug=slug)
    try:
         selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        return render(request, 'polls/detail.html', {
            'question': question,
            'error_message': "Nie wybrałeś żadnej opcji.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(question.slug,)))

#other views

polls/models.py:

from django.utils import timezone
from django.db import models
from django.contrib import admin
from django.urls import reverse

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    slug = models.SlugField(null=True)

    def __str__(self):
        return self.question_text

    def get_absolute_url(self):
        return reverse('polls:detail', kwargs={'slug': self.slug})

    #...

#other models

polls/urls.py:

from django.urls import path
from . import views

app_name = 'polls'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<slug:slug>/', views.DetailView.as_view(), name='detail'),
    path('<slug:slug>/wyniki', views.ResultsView.as_view(), name='results'),
    path('<slug:slug>/glosowanie', views.vote, name='vote'),
    #...
]

polls/templates/polls/detail.html:

<body>
#...

<form action="{% url 'polls:vote' question.slug %}" method="POST">
{% csrf_token %}
<fieldset>
    <legend><h3>{{ question.question_text }}</h3></legend>
    {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
    {% for choice in question.choice_set.all %}
        <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
        <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
    {% endfor %}
</fieldset>
    <br>
<input class="btn btn-primary" type="submit" value="Głosuj">
</form>

#...
</body>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source