OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

how to stay in the same route in python flask?

  • Thread starter Thread starter M4G
  • Start date Start date
M

M4G

Guest
I have added a route to the flask app, but it seems I can't get the results on the browser:

Code:
# A very simple Flask Hello World app for you to get started with...
#
from flask import Flask, request

from processing import do_calc_melting2


app = Flask(__name__)
app.config["DEBUG"] = True

@app.route("/melting2", methods=["GET", "POST"])
def add_page_melting():
    errors = ""
    if request.method == "POST":
        number1 = None
        number2 = None
        number3 = None
        try:
            number1 = float(request.form["number1"])
        except:
            errors += "<p>{!r} is not a number.</p>\n".format(request.form["number1"])
        try:
            number2 = float(request.form["number2"])
        except:
            errors += "<p>{!r} is not a number.</p>\n".format(request.form["number2"])
        try:
            number3 = float(request.form["number3"])
        except:
            errors += "<p>{!r} is not a number.</p>\n".format(request.form["number3"])
        if number1 is not None and number2 is not None and number3 is not None:
            result = do_calc_melting2(number1, number2, number3)
            return '''
                <html>
                    <body>
                        <p>The result is {result}</p>
                        <p><a href="/melting2">Click here to calculate again</a>
                    </body>
                </html>
            '''.format(result)
    return '''
        <html>
                <form method="post" action=".">
                    <p><p>Number 1</p><input name="number1" /></p>
                    <p><p>Number 2</p><input name="number2" /></p>
                    <p><p>Number 3</p><input name="number3" /></p>
                    <p><input type="submit" value="Do calculation" /></p>
                </form>
            </body>
        </html>
    '''.format(errors=errors)

When I use @app.route("/") everything works just fine, but when I use different route then the post part works, then it jumps to "/" route.
<p>I have added a route to the flask app, but it seems I can't get the results on the browser:</p>
<pre><code># A very simple Flask Hello World app for you to get started with...
#
from flask import Flask, request

from processing import do_calc_melting2


app = Flask(__name__)
app.config["DEBUG"] = True

@app.route("/melting2", methods=["GET", "POST"])
def add_page_melting():
errors = ""
if request.method == "POST":
number1 = None
number2 = None
number3 = None
try:
number1 = float(request.form["number1"])
except:
errors += "<p>{!r} is not a number.</p>\n".format(request.form["number1"])
try:
number2 = float(request.form["number2"])
except:
errors += "<p>{!r} is not a number.</p>\n".format(request.form["number2"])
try:
number3 = float(request.form["number3"])
except:
errors += "<p>{!r} is not a number.</p>\n".format(request.form["number3"])
if number1 is not None and number2 is not None and number3 is not None:
result = do_calc_melting2(number1, number2, number3)
return '''
<html>
<body>
<p>The result is {result}</p>
<p><a href="/melting2">Click here to calculate again</a>
</body>
</html>
'''.format(result)
return '''
<html>
<form method="post" action=".">
<p><p>Number 1</p><input name="number1" /></p>
<p><p>Number 2</p><input name="number2" /></p>
<p><p>Number 3</p><input name="number3" /></p>
<p><input type="submit" value="Do calculation" /></p>
</form>
</body>
</html>
'''.format(errors=errors)
</code></pre>
<p>When I use @app.route("/") everything works just fine, but when I use different route then the post part works, then it jumps to "/" route.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top