简体   繁体   中英

TypeError when passing a variable from one function to another in Flask

I have created 2 routes in my Flask App that takes in value from the first function and then the further operations are done in the second route but I get an Error stating TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Here is my routes.py file

@app.route('/getdetails', methods=['GET', 'POST'])
def getdetails():
    if request.method == 'POST':
        name = request.form['name']
        sems = request.form['sems']
        sems=int(sems)
        return render_template('new.html', n=name, s=sems)
    return render_template('index.html')

@app.route('/getmarks', methods=['GET', 'POST'])
def getmarks():
    sems = request.args.get('sems')
    sems = int(sems)
    for i in range(sems):
        if request.method == 'POST':
            sgp = request.form['sgpa']
            sgp = int(sgp)
            marks.append(sgp)
    return render_template('getmarks.html', s=sems, marks=marks)

also here is the html template

{% block content %}

<form name="inputmarks" action="/getmarks" method="POST" style="width: 500px; margin: auto">
    {% for i in range(s) %}
        <label>SGPA {{i}}: </label>
        <input type="text" name="sgpa" class="form-control">
        <input type="submit" value="Lets Go">
    {% endfor %}    
</form>

The variable that I am requesting is of type 'int' but then when I print the type it shows me type as 'None Type' How do I fix this?

variable sems is the value I am requesting for

The error is probably arising from the second of these two lines:

sgp = request.form['sgpa']
sgp = int(sgp)

The submitted form is either empty or is not passing in the text value.

Try adding a print statement to check the first value of sgp

sgp = request.form['sgpa']
print('sgp =', sgp)
sgp = int(sgp)

The problem is, most likely, in the begining of the getmarks function.

@app.route('/getmarks', methods=['GET', 'POST'])
def getmarks():
    sems = request.args.get('sems')
    sems = int(sems)

The way you are retrieving the value sems is, most likely, inconsistent with the the form you provided, and as a result, sems ends up being None , leading to the error in the call to int() .

Why does this happen? It is because posted data is not persistent across HTTP calls. The fact the the request handled by getdetails had the value of the field sems posted to it (I'm assuming it was, even though you did not provide the HTML form for that), does not mean that the same value would automatically get passed (either as a value in form or args ) to subsequent HTTP requests, including the one handled by getmarks .

You need to embed any arguments posted from the first form, which you want to be posted by the second form, inside the second form. You can do this in several ways:

  • You can create an invisible field in the second form, with the same name as it had in the first form, and set it to an initial value equal to the value posted by the first request. This would require that you access this field via the form property of the Flask request, rather than through the args property.
  • You can make the value part of the form target as a GET-style argument. I would not recommend this method, though.

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