title: flask session variable doesn't change after a request
body: <p>I want to update the variable('CAPTCHA_NUM') when I visit the url('/captcha') and get this variable in templates: </p>
  
  <pre><code>@app.route('/captcha')
  def captcha():
      from captcha.image import ImageCaptcha
      from random import random
      image = ImageCaptcha()
      x = int(round(random() * 10000))
      if x &lt; 1000: x += 1000
      session['CAPTCHA_NUM'] = x
      return image.generate(str(session['CAPTCHA_NUM']), 'png').getvalue()
  </code></pre>
  
  <p>But each time when I refresh my page(visit this url), the captcha updates, however the <code>session['CAPTCHA_NUM']</code> doesn't. </p>
  
  <p>For example, first I get the captcha <code>7530</code>, then I refresh <code>register.html</code>(which visits the url) and captcha is updated but the <code>session['CAPTCHA_NUM']</code> still is <code>7530</code>.</p>
  
  <p>And this is where I visit the url(captcha):</p>
  
  <p>register.html</p>
  
  <pre><code>&lt;img id="vcode" src="{{url_for('captcha')}}" alt="captcha" /&gt;
  </code></pre>
  
  <p>and I get <code>session['CAPTCHA_NUM']</code> in <code>validate_capcha()</code>:</p>
  
  <pre><code>&lt;input type="text" name="captcha" onblur="validate_captcha()" /&gt;
  </code></pre>
  
  <p>=================Solution========================</p>
  
  <p>In the official doc of <a href="http://flask.pocoo.org/docs/0.10/templating/#standard-context" rel="nofollow">flask session</a>, it says:</p>
  
  <p><code>This variable is unavailable if the template was rendered without an active request context.</code></p>
  
  <p>I just change the <code>src</code> attribute of <code>img</code> tag, and apparently this isn't an active request context.</p>
  
  <p>And finally I put the captcha validation part at the server-side so that I can avoid this problem.</p>