Web.py template error: "sum" does not exist

I use the built-in "sum" function in the web.py template template and get the following error:

global name 'sum' is not defined

Source code below:

$if profs:
   $for prof in profs:
      $sum([1, 2, 3])

I can use "sum" just fine in Python REPL in the terminal.

What could be the problem?

Thanks Jacob

+3
source share
2 answers

Add functions to dict and pass as an argument to globals for rendering:

render = web.template.render('templates/', globals={'sum': sum})

Then in your template, you can simply use it:

$def with (numbers)

<h1>Numbers add to $sum(numbers)</h1>
+4
source

Not all python codes are available in template notation, try something like this:

$if profs:
   $for prof in profs:
      $code:
         mysum = sum([1, 2, 3])
      $mysum
0
source

Source: https://habr.com/ru/post/1722968/


All Articles