简体   繁体   中英

for loop inside a flask function -Internal server error

I am trying to print fibonacci series of a number using flask framework and jinja templates. The following program is giving me a 505 Internal server error! But when i comment the for loop inside the function,it gives me the correct result

fibonacciURL.py

from flask import Flask
from jinja2 import Environment, PackageLoader

app= Flask(__name__)

def fib(num):
    if num==1 or num==0:
        return num
    else:
        return fib(num-1)+fib(num-2)

@app.route('/fib/<number>')
def generate_fibonacci(number):

    env= Environment(loader=PackageLoader('Fibonacci','templates'))
    fibMap={}
   #for x in range(number):
   #      fibMap[i]=fib(i)


    for i in range(0,number):
       print 'Hello'
#pass

    template= env.get_template('table_template.html')
    return template.render(num=3,map={1:1,2:2,3:3})
#return 'wda'    

if __name__ =='__main__':
    app.run(host='0.0.0.0')

my table_template.html is

<table>
   {%for i in range(1,num)%}
    <tr>
    <td>{{i}}</td>
    <td>{{map[i]}}</td>
    </tr>
   {% endfor %}

</table>

Without the for loop,the page is showing the expected result

The route parameter <number> is given to you as a string , not an integer.

If you want Flask to give you a numeric value, tell it so in the route configuration:

@app.route('/fib/<int:number>')

See the Variable Rules section in the quickstart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM