-1

I have been working with flask for a long while, but after a break from it, I cant seem to figure out what's wrong here.

index.html:

<input name="linkHolder" type="url" class="defaultTextBox advancedSearchTextBox link" placeholder="http://www.youtube.com">

<form method="POST" action="/button">
    <input class="btn" type="submit">Go</input>
</form>

main.py:

  @app.route('/button', methods=["GET", "POST"])
  def button():
    if request.method == "POST":
      dlink = request.form.get("linkHolder")
      print(dlink)
      return render_template("index.html", dlink=dlink)

I'm sorry if its a simple answer but my end goal here is to load the link typed by the user, print said link, and then reload the page. What am I doing wrong?

2 Answers2

1

In your index.html, your <form> tag does not include the linkHolder input. Do the following:

<form method="POST" action="/button">
    <input name="linkHolder" type="url" class="defaultTextBox advancedSearchTextBox link" placeholder="http://www.youtube.com">
    <input class="btn" type="submit">Go</input>
</form>

You might also need an if statement in main.py that actually renders the page

  @app.route('/button', methods=["GET", "POST"])
  def button():
    if request.method == "GET":
        return render_template("index.html")
    if request.method == "POST":
      dlink = request.form.get("linkHolder")
      print(dlink)
      return render_template("index.html", dlink=dlink)
Paulina Khew
  • 397
  • 4
  • 13
0

You need a form with the name Also, your tag </input> input doesn't have any closing tag you should be using button tag

<form method="POST" action="/button">
    <input type="text" name="linkHolder">
    <button class="btn" type="submit">Go</button>
</form>
Swapnil Soni
  • 965
  • 1
  • 10
  • 26