[Answer]-Passing variables from template to view

1👍

What you need to do is setup an Ajax request & handle the score etc in a django view.

Take a read of this & it should give you everything you need; http://www.tangowithdjango.com/book/chapters/ajax.html

You’ll probably end up with JS a bit like this;

{% block content %}
<script>
  /* global $ */
  $(document).ready(function() {
    'use strict';
    $(window).on('message', function(evt) {
      //Note that messages from all origins are accepted
      //Get data from sent message
      var msg = evt.originalEvent.data;
      if(msg.messageType == "SCORE")
      {
        $.get('/game/save_score/', {score: msg.score}, function(data){
           $('#score').html(data);
       });
      }
    });
  });
</script> 
<iframe id="game_iframe" src={{gameurl}}></iframe>
{% endblock %}

And a view;

def save_score(request):
    context = RequestContext(request)
    score = None
    if request.method == 'GET':
        score = request.GET['score']

    # Do whatever you need to save the score.

    return HttpResponse(score)

Leave a comment