title: Getting the same context variable in two template blocks
body: <p>I have a django template tag that sets a context variable (it gets a random image from a model, but for this example, lets say it gets a random number)</p>
  
  <pre><code>{% get_random_number %}
  {{ my_random_number }} &lt;!-- outputs a random number between 1 and 10 --&gt;
  </code></pre>
  
  <p>This works fine.</p>
  
  <p>However I need to get the same 'random' number in two different blocks within my page:</p>
  
  <pre><code>{% block block1 %}
    {% get_random_number %}
    {{ my_random_number }} &lt;!-- outputs a random number between 1 and 10 --&gt;
  {% endblock %}
  
  {% block block2 %}
    {% get_random_number %}
    {{ my_random_number }} &lt;!-- outputs a random number between 1 and 10 --&gt;
  {% endblock %}
  </code></pre>
  
  <p>This obviously doesn't work as I get two different results (unless by chance, they're the same!)</p>
  
  <p>So how do you use a templatetag to set a context variable that's consistent across two template blocks? </p>
  
  <p>doing this doesn't work - the context variable is limited to the block it's created in...</p>
  
  <pre><code>{% get_random_number %}
  
  {% block block1 %}
    {{ my_random_number }}
  {% endblock %}
  
  {% block block2 %}
    {{ my_random_number }}
  {% endblock %}
  </code></pre>
  
  <p>So.. how can I 'save' the initial result somewhere else, then recall it, if it's already been generated earlier in the call?</p>
  
  <p>Thanks</p>