0

I'm new to flask and I'm making a webpage, where the user submits something and I need to run scripts with the using of the entered value.

2 questions:

Why don't make_nil gets called? - I'm getting the same value I entered.

Why don't i see sys.stdout('age: ', age) or print('age: ', age)?

Thanks!

flask_ager.py:

from flask import Flask, render_template, request
import sys
app = Flask(__name__)

@app.route('/send', methods=['GET', 'POST'])
def send():
    if request.method == 'POST':
        kapa = request.form['age']
        age = make_nil(int(kapa))
        sys.stdout('age: ', age)
        return render_template('age.html', age=age)
    return render_template('inka.html')



def make_nil(age):
    return 0 

if __name__ == "__main__":
    app.run()

age.html:

<!DOCTYPE html>
<html>
<head> Siker? </head>
<body> 
<h1> Your age is {{age}} </h1>
</body> 
</html>

inka.html:

<!DOCTYPE html>
<html>
<head> Siker? </head>
<body> 
<h1> How old are you? </h1>
<form method="POST" action="/send">
<div class="form_group">
<input type="text" name="age">
</div>
<input class="btn btn-primary" type="submit" value="submit">
</form>
</body> 
</html>
Marci
  • 427
  • 2
  • 9
  • 20
  • Have you tried debugging to see where your code is going? I'd put a breakpoint on your if statement to see if it even enters. Also try doing a print(request.method) before your if statement to see what that outputs. – CodeLikeBeaker Dec 06 '17 at 22:15
  • print(request.method) doesn't print anything to the cmd. The only thing I see in cmd is GET/POST, GET/POST – Marci Dec 06 '17 at 22:29

1 Answers1

1

When I test this, I get the following:

ERROR in app: Exception on /send [POST]
...
  File "flask_ager.py", line 10, in send
    sys.stdout('age: ', age)
TypeError: 'file' object is not callable

This line should be:

sys.stdout.write('age: {}'.format(age))

Updating that line and retesting shows the app is working correctly:

enter image description here

Matt Healy
  • 18,033
  • 4
  • 56
  • 56