-5

HTML Code

    <!DOCTYPE html>
<html>
  <head>
    <h1>App</h1>
  </head>
  <body>
    <form action="Test.py" method="GET">
      <label for="search">Enter keyword here</label>
      <input type="text" name="userInput" required></input>
      <input type="submit" value="submit"></input>
      </form>
    
  </body>
</html>

Flask Code

    from flask import Flask, url_for, request

app = Flask(__name__)
@app.route("extPage.html", methods=['POST', 'GET'])

userinput = request.form("userInput")
print(userinput)

Whenever I run the HTML and submit something, it just returns the python flask code on the webpage. Want I am trying to do is use the userInput tag to create a variable to use in a python program not related. I need the input returned as a string which is why I am testing it with "print(userinput). My goal is to print the submission.

Nick Rizzolo
  • 125
  • 1
  • 2
  • 12
  • Your "flask code" is not a proper flask code. You have a decorator without a function. That thing should be a syntax error. That's also not how you link flask actions – h4z3 Oct 13 '21 at 14:13
  • I am curious. When you say - run the HTML, what do you do? Do you navigate to the HTML file and load in the browser? – sushant Oct 13 '21 at 14:14
  • @sushant I'd assume OP does this. Because the html opens, while flask code is literally un-runnable because of decorator in the middle of the thing and not even a flask server started. And the code file somehow opens too when submitting the form, so the files are not served by the server but rather filesystem – h4z3 Oct 13 '21 at 14:16
  • @h4z3 Noted. What I do not understand is how does OP get the Python code printed when the browser loads the HTML page. – sushant Oct 13 '21 at 14:18
  • I run the HTML file, type a random string in the submit box and when I press submit it print's out the flask code. @h4z3 Yes, I felt something was wrong, the flask app is confusing me. What exactly is a decorator? Also I included the / in the route. – Nick Rizzolo Oct 13 '21 at 14:22
  • @sushant Action is basically a redirect. If files are in the same directory, then it "links" to the file. Text files can be opened in browsers. – h4z3 Oct 13 '21 at 14:25
  • @h4z3 I missed that part when OP said that the "submit" button was clicked. Thank you. – sushant Oct 13 '21 at 14:34
  • @h4z3 Oh I thought action sent the the input to the file. – Nick Rizzolo Oct 13 '21 at 14:36

1 Answers1

1

Place the print statement within a function below the route decorator. something like:

#Don't call html pages. Call routes that return html pages
@app.route("/extPage/", methods=['POST', 'GET']) 
def index():
    userinput = request.form("userInput")
    print(userinput)
    return render_template('return_page.html', ui=userinput)

You can find the official documentation's tutorial here. It's what helped me the most when I have gotten stuck.

Arkiralor
  • 51
  • 1
  • 8