0

I am developing a human vs human chess app in django. The part for pawn promotion is not working. The session values are changing,but not getting updated to django template.

The promote view

def promote(request):
    #Update board with promoted piece
    board = request.session['board']
    target = request.session['promote_target']
    board[target[0]][target[1]] = request.POST['piece']
    request.session['board'] = board
    request.session['promotion'] = False
    request.session['player'] = 1

    return render(request,'chess_app/Default.htm')

The js Function to call server

function promotion(piece){
        //Function to promote pawns
        //Add a confirm message
        $.ajax({url:"{%url 'promote'%}",data:{'piece':piece},type:'post',success:function(){location.reload()}});
        }

Everything works fine, but the session is not getting updated It would be great if you can help.

  • You need to show the piece of code where you send the data to the template. – Daniel Roseman Oct 16 '17 at 15:10
  • And you dont really need `request.session.modified = True` unless you are not modifying a nested key argument. All of your arguments are top level for session object – MohitC Oct 16 '17 at 15:11
  • You are going to add checks server-side to make sure the client isn't faking that request, right? – Jon Clements Oct 16 '17 at 15:14
  • Ya, I know, I ran into this bug and I thought to resolve the bug first then do rest. Besides this is a hobby project, don't need that kind of heavy security – user3764953 Oct 16 '17 at 15:17
  • I saw request.session.modified = True in a related post and I thought why not try it? But no luck :( – user3764953 Oct 16 '17 at 15:19
  • @DanielRoseman, isn't this the function that sends the data into the template. If not can you be more specific – user3764953 Oct 19 '17 at 14:19
  • Well no. Somewhere you have some code that takes board, promotion and player and passes them into a template context so that it can display them, no? – Daniel Roseman Oct 19 '17 at 14:25

2 Answers2

0

Check this question I guess it should resolve your problem. However IMHO use session in template is not very good solution, check this alternative options:

Middleware

You can get values from session and set it in request object in middleware. This option is sensible if you planning to use this values in different views and different templates.

View context

You can put values in view context. This option will be good if you planning to use values only in one view. (And of course you can create a mixin to share this functionality between different view

Inclusion tags

If you can extract part of template that use this data, you can create custom tag for this template and get all required data from request.

Context processor

If you need to share this values between all templates you may use context processors.

Kirill Ermolov
  • 780
  • 6
  • 19
0

I'm not sure why this is not posted in this thread after being asked like a year ago.

The session values are changing, but not getting updated to django template.

To fix this you simply tell Django that sessions have been modified and then it knows to update them in the template:

# make Django update sessions in templates
request.session.modified = True

Here are the docs:

https://docs.djangoproject.com/en/2.2/topics/http/sessions/#when-sessions-are-saved

So to put this in context:

def promote(request):
    #Update board with promoted piece
    board = request.session['board']
    target = request.session['promote_target']
    board[target[0]][target[1]] = request.POST['piece']
    request.session['board'] = board
    request.session['promotion'] = False
    request.session['player'] = 1

    # make Django update sessions in templates
    request.session.modified = True

    return render(request,'chess_app/Default.htm') # you are good
ViaTech
  • 2,143
  • 1
  • 16
  • 51