0

I am getting the error message UndefinedError: 'mod' is undefined

In this scenario, card is always between 0 and 51.

Notice there is a base.html template involved.

Where do I put the javascript in the following situation or can I construct it differently?

Javascript:

numbers = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['S','H','D','C']

function mod(card){
    return numbers[card%13]+suits[card%4]
}

HTML:

{% for card in cardlist %}
    <input type="radio" class="btn" name="person" value="{{ card }}"/>
    {%set temp=mod(card)%}temp
{% endfor %}

base.html

<html>
  <head>
    {% block head %}
    <link rel="shortcut icon" href="/static/images/favicon.ico" type="image/x-icon" />
    <link rel="icon" href="/static/images/favicon.ico" type="image/x-icon">
    <title>{% block title %}{% endblock %} - BridgeToGo</title>
    {% endblock %}
  </head>
  <body>
<script>
function goBack() {
    window.history.go(-1);
}
</script>

  </body>
</html>
terrymorse
  • 6,771
  • 1
  • 21
  • 27
zerowords
  • 2,915
  • 4
  • 29
  • 44
  • I'd guess this is something to do with the scope of the JavaScript function. is `mod` function in the global scope? – William Isted May 28 '20 at 14:00
  • 2
    `{%set temp=mod(card)%}` <= what is this syntax? It looks like you are trying to call a `mod` method in something other than javascript. – Taplar May 28 '20 at 14:03
  • Yes, I am calling mod() from Jinja2 inside a template in a loop. `{% for card in cardlist %}` ... `{% endfor %}` where cardlist is some integers from 0..51. Is there a better way to do this. – zerowords May 28 '20 at 14:27
  • `mod` function is in the – zerowords May 28 '20 at 14:31
  • 1
    You can't call a JavaScript function from jinja2. JavaScript runs in the browser, but jinja2 runs on the server. See [What is the difference between client-side and server-side programming?](https://stackoverflow.com/a/13840431/3113485) – terrymorse May 28 '20 at 15:03
  • It seems I must do the mod() in my python. Pity. – zerowords May 28 '20 at 15:19

0 Answers0