-1

How do I reference input in a python flask code, for some reason request.form doesn't seem to be working. Here's the code:

@app.route("/post_field", methods=["GET", "POST"])
def need_input():
    forum = ['def']
    inputf = request.form["fname"]

    if inputf == 'test':
        forum.append(inputf)

    return """
        <html>
        <body>
            <p>
            <font face="Times New Roman" size="+2" color="#870012">{forum}</font>
            <form method="post" action=".">
            <p>
            <input id="fname" name="fname"</p>
            </form>
    </body>
    </html>
    """.format(forum=forum)
KetZoomer
  • 2,701
  • 3
  • 15
  • 43
Jack P
  • 1

2 Answers2

1

I believe this is what you want:

number = 0

@app.route("/post_field", methods=["GET", "POST"])
def need_input():
    if request.method == "POST":
        if "fname" in request.form:
            number += int(request.form["fname"])
    else if request.method == "GET":

        return f"""
            <html>
            <body>
                <p style="font-family: 'Times New Roman'; size: 2vw; color: '#870012'">{number}</p>
                <form method="post">
                    <input id="fname" name="fname"</p>
                </form>
             </body>
            </html>
        """
KetZoomer
  • 2,701
  • 3
  • 15
  • 43
-1

If you want to get a specific field from the form, you need to use:

request.form.get('fname')

You'd be much better off using Jinja and Render Template rather than doing it this way. It would make your code much more scalable.

vigviswa
  • 157
  • 1
  • 8