Django session variable value not up to date in view

i have two views to deal with some step processes.

  • step 1: show all items with checkboxes, use ajax send seleted item's pk to backend by click button. There is a function at backend to set those values into session.
    def selecteditems(request):
    if request.is_ajax and request.method == "POST":
        request.session['selected'] = request.POST.getlist('items[]')
        request.session.modified = True
        return HttpResponse(status=200)
  • step 2: get those variables from session and put them into second view.
    def step2(request):
    if request.method == "GET":
        selectedList = request.session.get('selected')
        allItems = Items.objects.all()
        selectedItem = allItems.filter(id__in=selectedList)
        context = {
            "selectedItem": selectedItem,
        return render(request, 'step2.html', context)

The problem i got is that, the second view always got "delay" values, meanning i have to refresh my page to load the latest items that i just selected, otherwise it is the data that i select in last time.

Answers: 0