1

I am learning to use sessions in Django. Here is simple code that I wrote. It was working yesterday, however, I keep getting KeyErrors 'score' today. Where is the problem?

def home(request): request.session.get('score',0) if request.GET.get('add'): request.session['score'] += 1 if request.GET.get('restart'): request.session['score'] = 0 return render_to_response('home.html', {'zero':request.session['score']}) 
2
  • see what is the output of request.session? Commented Oct 22, 2016 at 6:09
  • I had the same issue with my code. It is weird though why it works in the beginning and it doesn't work after a while. No code has been changed. Commented Nov 8, 2017 at 14:16

1 Answer 1

4

Change request.session.get('score',0) to request.session.setdefault('score',0).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.