how I can pass data from ajax function to another context function in class(TemplateView)

I should get data from ajax, pass it to class that render this html page and update data, but don’t refresh the page. I get this data from ajax but I can’t pass it to Context.

I have this html:

{% extends 'main/base.html' %}
{% load static %}


{% block content %}
   <div class="container">
        <div class="choose_buttons">
            <a href="{% url 'start_game' %}"
            hx-get="{% url 'start_game' %}"
            hx-target=".view_page">
            <div class="play_keys" id="paper"><img src="{% static 'main/img/paper.jpeg' %}" alt=""></div></a>

            <a href="{% url 'start_game' %}"
            hx-get="{% url 'start_game' %}"
            hx-target=".view_page"><div class="play_keys" id="scissor"><img src="{% static 'main/img/scissor.jpeg' %}" alt=""></div></a>

            <a href="{% url 'start_game' %}"
            hx-get="{% url 'start_game' %}"
            hx-target=".view_page"><div class="play_keys" id="rock"><img src="{% static 'main/img/rock.jpeg' %}" alt=""></div></a>
        </div>

        key: {{ some_data }}
   </div>
   <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
   <script>
    $(document).ready(function() {
        $(".play_keys").off().on('click', function()  {
            var id = $(this).attr("id");
            $.ajax({
            url: "{% url 'get_playbutton' %}",
            type: "GET",
            cache: false,
            data: { "id": id },
            success: function(response) {
            },
            });
        });
    });
   </script>
{% endblock %}

views.py:

def ajax_get_playbutton(request):
    id_button = request.GET.get("id")
    game_ = startGame()
    game_.playground(id_button)
    return HttpResponse("ok")

class startGame(TemplateView):
    template_name = 'main/game_field.html'

    def playground(self, key):
        self.choose = key
        #another data ...

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['choos_data'] = self.choose

        return context

urls.py:

urlpatterns = [
    path('game-room', views.startGame.as_view(), name= "start_game"),
    path('ajax_get_playbutton', views.ajax_get_playbutton, name= "get_playbutton"),    
]

Side note: When posting code, templates, or error messages here, enclose the text between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep that text properly formatted. (I’ve taken the liberty of changing this post for you.)

Can you be a bit more explicit about what you’re trying to do here and what isn’t working? It’s not clear from your description what you’re trying to ask.

Im sorry for my English, I try to explain:

I’m trying to pass data from an ajax request to the view class that displays this page. I receive the data from the request and also this data is transferred to one of the functions of this class, but when I try to transfer the received data from the function (playground) to the context function (get_context_data) it gives the following error (‘startGame’ object has no attribute ‘choose’)“”"

def ajax_get_playbutton(request):
    id_button = request.GET.get("id")
    game_ = startGame()
    game_.playground(id_button)
    return HttpResponse("ok")

class startGame(TemplateView):
    template_name = 'main/game_field.html'

    def playground(self, key):
        self.choose = key
        #another data ...

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['choos_data'] = self.choose #<- this data doesn't exist here

        return context

First, whenever you’re asking a question here about an error being received, please post the complete traceback of the error.

Now, a class based view (CBV) isn’t just a function based view (FBV) wrapped up in a class. If you want to use a CBV from a different view, you need to call it in the same way that the url dispatcher would call it.

But my first question would really be to ask why you’re trying to call that CBV from the FBV, and not having the AJAX call the CBV directly.