Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
Refreshing context variable with ajax

getting null instead of the refreshed value when the event is triggered in the template, getting the correct value in the terminal for example success 6. How do I get the real value to render out instead of null, feel free to improve this question if you see anything wrong. Cheer!

I've searched a lot in the community and tried lots of things but couldn't find what's causing the issue, I hope someone can help me!

reproducible repo: https://github.com/bettstogain/reproducible_shop/tree/main/env

This is my code:

path('ajax_update/', views.ajax_update, name="ajax_update"),

view

#bagstuff is a function that sees if the user is authenticated and does get_or_create
#or is not authenticated and refers to use the cookie

def ajax_update(request):
    stuff = bagstuff(request)
    bag = stuff['bag']

    # if_ajax returns, request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
    if is_ajax and request.method == "GET":

        #trying to get the bag value after the event is triggered
        if bag:
            print("success", bag)

            #trying to get the bag and put it in JsonResponse
            data_attribute = request.GET.get(bag)
            #print(data_attribute) -- this prints null

        return JsonResponse(data_attribute, safe=False, status=200)
    else:
        print("failed")

    context = {"bag": bag}

template:

<script>
    var update_bag_url = "{% url 'store:ajax_update' %}";
</script>

javascript:

function update_bag() {
    // optional: don't cache ajax to force the content to be fresh
    $.ajaxSetup ({
        cache: false
    });
    // specify loading spinner
    var spinner = "<img alt='loading..' />";

    $("#lil-icon-two").html(spinner).load(update_bag_url);
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda